- substr: documentation ( source)
- stripos: documentation ( source)
<?php
$html = "This is a test";
$search = "is";
$pos = 0;
$loop = true;
while($loop) {
# Check if the word can be found in the text
$pos = stripos($html,$search,$pos);
if($pos !== false) {
$spanStart = "<span style='background-color:khaki;'>";
$spanEnd = "</span>";
$html = substr($html,0,$pos) . $spanStart . substr($html,$pos,strlen($search)) . $spanEnd . substr($html,$pos+strlen($search),strlen($html));
$pos = $pos + strlen($search) + strlen($spanStart) + strlen($spanEnd);
} else {
$loop = false;
}
}
echo "html=[$html]";