Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I replace all the occurrences of the $keyword within the string without replacing the keywords found within URLs and image tags?
    text
    copied!<p>How can I replace <strong>all</strong> the occurrences of the <strong>$keyword</strong> within a string <strong>without</strong> replacing the keywords found within the hyperlink URLs, image tag URLs, image tag title and alt tags?</p> <p>Example:</p> <pre><code>$keywords = 'sports'; $string = '&lt;a href="http://my_domain_name.com/sports/info.php"&gt;&lt;img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news" /&gt;Football sports news&lt;/a&gt;'; </code></pre> <p>Notice that the keyword '<strong>sports</strong>' appears with the hyperlink URL, images tag URL, and image tag title and alt tags.</p> <p>I want to replace $keywords (sports) with:</p> <pre><code>&lt;span style="color: #000000; background-color: #FFFF00; font-weight: normal;"&gt;sports&lt;/span&gt; </code></pre> <p>to yeild the following results:</p> <pre><code>&lt;a href="http://my_domain_name.com/sports/info.php"&gt;&lt;img class="icon" src="http://my_domain_name.com/sports/images/football.gif" title="Get the latest football sports news" alt="Get the latest football sports news" /&gt;Football &lt;span style="color: #000000; background-color: #FFFF00; font-weight: normal;"&gt;sports&lt;/span&gt; news&lt;/a&gt; </code></pre> <p>Thanks in advance.</p> <p><strong>EDIT - Additional Information</strong></p> <p>Currently I am using the following 2-step method and it works for <strong>just the URLs</strong>, and <strong>not</strong> the title and alt tags. I also need to <strong>not</strong> replace the keywords in the title and alt tags too.</p> <pre><code>// Replaces both the website and general images path urls with character strings (used to prevent highlighting keywords found within the path urls) if(strpos('http://my_domain_name.com/sports', $keywords) != false) { $description = str_ireplace('http://my_domain_name.com/sports', '1q2w3e4r5t6y7u', $description); } if(strpos('http://my_domain_name.com/sports/images', $keywords) != false) { $description = str_ireplace('http://my_domain_name.com/sports/images', '7u6y5t4r3e2w1q', $description); } // Highlights the Search Keywords $description = str_ireplace($keywords, '&lt;span style="color: #000000; background-color: #FFFF00; font-weight: normal;"&gt;'.$keywords.'&lt;/span&gt;', $description); // Replaces the character strings with the website and general images path urls if(strpos('http://my_domain_name.com/sports', $keywords) != false) { $description = str_ireplace('1q2w3e4r5t6y7u', 'http://my_domain_name.com/sports', $description); } if(strpos('http://my_domain_name.com/sports/images', $keywords) != false) { $description = str_ireplace('7u6y5t4r3e2w1q', 'http://my_domain_name.com/sports/images', $description); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload