I have:
$keyword = 'blue';
I want to insert a special class when in a string the system found blue
.
This sky is <span class="blue">blue</a>
.
This pear is green
.
$string = "The sky is blue.";
if (stripos($string, $keyword) !== false) {
// The string is found, now change the class
}
Thanks a lot.
Is this what you are looking for?
$keyword = 'blue';
$replace = 'green';
$string = "The sky is blue.";
if (stripos($string, $keyword) !== false) {
$string = str_ireplace(array($keyword), $replace , $string);
}
You could use regexps, as in:
function format_keywords($html, $keywords) {
foreach ($keywords as $keyword) {
$keyword = preg_quote($keyword);
$html = preg_replace("/\b$keyword\b/i", "<span class=\"$keyword\">$keyword</span>", $html);
}
return $html;
}
For example, this:
format_keywords("This sky is blue.", array("is", "blue"));
Would yield:
This sky <span class="is">is</span> <span class="blue">blue</span>?
Note that This
has not been recognized, thanks to the \b
delimiters.
See the documentation: