Hi, can someone help me to write the right regular expression for the following. Let we have the folowing text sourse This is example text with keyword and also <a href="http://www.somekeyword.com">linking page</a> and so on.. Code (markup): I want the keyword "keyword" to be replaced with "<b>keyword</b>" We I use the following: $text = // the text source above $text = preg_replace('/keyword/i', '<b>\\0</b>', $text); PHP: it returns This is example text with <b>keyword</b> and also <a href="http://www.some<b>keyword</b>.com">linking page</a> and so on.. Code (markup): I want the "keyword" in the url NOT to be replaced. Any advice how can do this?
Please Use This this may work according to your requirement. <?php $text = 'This is example text with keyword and also <a href="http://www.somekeyword.com">linking page</a> and so on..';// the text source above $text = preg_replace('/ keyword /i', '<b>\\0</b>', $text); echo $text; ?>
@shivakhanal thanks but your solution not helps me if we have for example 'somekeywod' not in the URL
please try this <? $text = // the text source above $text = preg_replace("#(^[<a])+(.+?)keyword#si"', '<b>\\0</b>', $text); ?> PHP: i dont try code but i think it will work explain is: ^[<a] = not start with <a