For a search query: Extract a relevant part of a given text that complies with the query and mark up words that are queried.
<?php //example $q = array('foobar','string'); $str = 'this is a random string with the text foobar - bla bla
some text should be marked up.';
echo markup($str,$q);
function markup($content,$q)
{
$pattern = array();
$content = str_replace('<',' ',$content);
$content = str_replace('>',' ',$content);
foreach($q as $v)
{
$pattern[] = "/\b".$v."\b/i";
}
$content = preg_replace($pattern,'<b>\0</b>',$content);
$content = preg_replace('/\s+/', ' ',$content);
return $content;
} ?>
Sign up to add your own comment here!
|
|