Source Rally PHP Community Scripts .. Sign up .. Login
For a search query: Extract a relevant part of a given text that complies with the query and mark up words that are queried.
Access: Public      Tags: snippet, markup, search, php
Add to favourites       Subscribe comments       Copy code       Bookmark
<?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('&lt;',' ',$content);
    
$content str_replace('&gt;',' ',$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;
}
?>
Add to favourites       Subscribe comments       Copy code       Bookmark
Sign up to add your own comment here!

Shared by:

regin

Mail user Add to friends
All user contributed content is available under the unless specified otherwise.
Remaining copyrights Regin Gaarsmand 2006-2008
About www.SourceRally.net