highlight word in php
This demo is for highlight word in string.
$str = 'aa ww rr tt Aa AA aa';
$search = 'aa';
echo highlight($search, $str );
function highlight($term, $target)
{
$terms = array_unique(explode(" ", $term)); // we only want to replace each term once
foreach ($terms as $term)
{
$target = preg_replace('/\b(' . preg_quote($term) . ')\b/i', "$1", $target);
}
return $target;
}
$str = 'aa ww rr tt Aa AA aa';
$search = 'aa';
echo highlight($search, $str );
function highlight($term, $target)
{
$terms = array_unique(explode(" ", $term)); // we only want to replace each term once
foreach ($terms as $term)
{
$target = preg_replace('/\b(' . preg_quote($term) . ')\b/i', "$1", $target);
}
return $target;
}
No comments:
Post a Comment