Inverse php preg

I created a template for matching a string of 3 numbers (for example: 333) between tags a:

@((<a>(.?[^(<\/a>)].?))*)([0-9]{3})(((.*?)?</a>))@i

How can I invert the pattern above to get numbers not between tags a.

I'm trying to use ?!but not working

Edit: Example input:

lor <a>111</a> em 222 ip <a><link />333</a> sum 444 do <a>x555</a> lo <a>z 666</a> res
+4
source share
2 answers

You are trying to solve an HTML problem in a text domain, which is just inconvenient to use. The correct way is to use the DOM parser; you can use the XPath expression to filter what you want:

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);

foreach ($xpath->query('//text()[not(ancestor::a)]') as $node) {
    if (preg_match('/\d{3}/', $node->textContent)) {
        // do stuff with $node->textContent;
    }
}
+5
source

kicaj, , , ....

regex html .

(. demo):

<a.*?</a>(*SKIP)(*F)|\d{3}

| <a ... </a>, . , , , .

, , , . 123 12345, lookahead lookbehind:

<a.*?<\/a>(*SKIP)(*F)|(?<!\d)\d{3}(?!\d)

( ) , s1, s2, s3...

0

Source: https://habr.com/ru/post/1543416/


All Articles