preg_replace to exclude PHP

2020-04-10 03:11发布

问题:

Im using preg_replace to replace keywords in text with a href tag, my regex is working awesome, right now my code is:

$newstring2 = preg_replace("/\p{L}*?".preg_quote($match[$i])."\p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring);

Only problem with this is, that I need to exclude any keywords inside <a href='https://keyword.cz' title="keyword">keyword</a>

This is what I found https://stackoverflow.com/a/22821650/4928816

So is here someone who can help me merge this two regex together?

Example:

$text = 'this is sample text about something what is text.'
$keyword = 'text'

Now thanks to my regex I get:

$text= 'this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.'

But If text is :

$text= 'this is sample <a href='text.php'>text</a> about something what is <a href='somelink.php'>text</a>.'

This is what for example I get:

$text= 'this is sample <a href='<a href='somelink.php'>text.php</a>'><a href='somelink.php'>text</a></a> about something what is <a href='somelink.php'><a href='somelink.php'>text</a></a>.'

Update: Why do I need this. Working on function to replace all keywords with specific URL in specific blog post full of tags.. For examle if

$keyword = 'key';

I need to find and replace full world with a href tag, for example: Key, Keyword, keyword, keylock, mykey, keys or also KeY, Keyword with UNICODE support

回答1:

If it must be done with regex I think PCRE verbs are your best option. Exclude all links then search for the term with word boundaries.

<a[\S\s]+?<\/a>(*SKIP)(*FAIL)|\bTERM\b

Demo: https://regex101.com/r/KlE1kc/1/

an example of a flaw with this though is if the a ever had a </a> in it. e.g. onclick='write("</a>")' a parser is really the best approach. There are a lot of gotchas with HTML and regexs.



回答2:

How about this with negative lookahead. Regex

Explanation: capture all the keyword that is called text and replace with it some link but don't capture those keywords that have </a> after it.

$re = '/(text)(?!<\/a>)/m';
$str = 'this is sample text about something what is text.

this is sample <a href=\'somelink.php\'>text</a> about something what is <a href=\'somelink.php\'>text</a>.';
$subst = '<a href=\'somelink.php\'>$1</a>';

$result = preg_replace($re, $subst, $str);

echo $result;

Output:

this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>. 

this is sample <a href='somelink.php'>text</a> about something what is <a href='somelink.php'>text</a>.

DEMO: https://3v4l.org/DVTB1