Answer the question
In order to leave comments, you need to log in
Regexp: negative lookback for tag membership
Faced a problem, it is necessary to wrap some phrases in the document in links. I wrote a regular tag, but here's the problem - I can't handle the exception when the phrase is already wrapped in a link.
<?php
$pattern = '#(?<!")('.$phrase.')(?!</a>)(?!</h1>)(?!</h2>)#iu';
$content = preg_replace($pattern, '<a href="'.$link.'" class="sph">$1</a>', $content);
?>
I added forward checks, but the problem is that if there are more words for the desired phrase, then the checks do not work.For example, a phrase of one word "One",
<a href="/test/">One</a>
in this context is processed normally, link:
<a href="/test/">One two</a> Can you tell me
how to check whether the text belongs to the link?
Answer the question
In order to leave comments, you need to log in
not strong in PHP, but in SQL, if a word represents part of a phrase, then it is taken in%. for example %One%.
You can split the content (preg_split) into fragments according to the separator "<a\s.*?", and then process each individual element of the split, and at the end, glue everything together.
Hmm, how about something like this:
$pattern = '#(?<!")[^<>]*('.$phrase.')[^<>]*(?!)(?! )(?!)#iu';
?
retrospective check works from the current position, that is, it is necessary to check not only
</a>but also
two</a>guess at once how to do it with a regular expression :) I suggest that it is better to take [ ^ < ] outside the tag *
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question