note 41209 deleted from function.preg-match-all by didou
| From: | didou@php.net | Date: | Thu, 22 Apr 2004 15:11:29 +0000 |
| Subject: | note 41209 deleted from function.preg-match-all by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-68534@lists.php.net to get a copy of this message | ||
Note Submitter: ingpancho@argentina.com
----
These functions will highlight words in a string, but ONLY those that are OUT of "<
>" tags.
For instance:
//INTO:
$buscaren="<b>Hello world</b><span class="yourstyle">Hello my
baby</span>";
//you can only select any of the words (or part of them)among "Hello world" and
"Hello my baby".
The second function its the main one, and is called by the first one which only splits the words to
search and calls several times to the second function.
You must send the target string ($buscaren) where words($buscarpalabras) must be searched into.
I wrote the function to send to it the words that you would type in a search box like "I'd
like to find this", then the function BuscarResaltar will split the string into words limited
by a space character and will search for them into $resaltaren, if it finds the word then the
function will add the tag $i_resaltarcon before de word and will add $f_resaltarcon after the word.
The only way to make this function crash is if you send a string to search in that is not a
gramatically correct html text: eg. "<text area<text contents</textarea>" the
function will not work if you want to highlight the word "contents".
One last thing: it won't highlight more than one time the same word.
function BuscarResaltar($resaltaren, $buscarpalabras, $i_resaltarcon="<strong>",
$f_resaltarcon="</strong>")
{
$buscarpalabras=SimplificarEspacios($buscarpalabras);
$buscar=explode(" ",$buscarpalabras);
$j=0;
while(strlen($buscar[$j])>0)
{
for($c=0;$c<$j;$c++)
{
//busco que no esté resaltando de nuevo algo ya resaltado
if(!strcasecmp($buscar[$c],$buscar[$j]))//devuelve 0 si son =
$noresaltar=1;
}
if (!$noresaltar)//osea que no lo he resaltado antes
$resaltaren=AgregarTagString($resaltaren, $buscar[$j], $i_resaltarcon, $f_resaltarcon);
$j++;
}
return $resaltaren;
}
function AgregarTagString($buscaren,$buscarpalabra, $i_resaltarcon="<strong>",
$f_resaltarcon="</strong>")
{
if (strlen($buscarpalabra)>0)
{
$cant=preg_match_all ("/".$buscarpalabra."/i", $buscaren,
$arr_palabras_encontradas);
//echo "Cantidad:" .$cant."<br>";
if ($cant>0)
{
$arr_cadenas_a_chequear=spliti($buscarpalabra,$buscaren);
$str_resultado=$arr_cadenas_a_chequear[0];
for($i=0;$i<$cant;$i++)
{
//echo "Palabras encontradas:".$arr_palabras_encontradas[0][$i] .
"<br>";
//echo "Palabras a chequear:".$str_resultado ."<br>";
$reemplazar="si";
$pos_initag=strrpos($str_resultado,"<");//posoción de la última aparición de
"<"
if(is_int($pos_initag))
{
$reemplazar="no";
$pos_fintag=strrpos($str_resultado,">");//posoción de la última aparición de
">"
if(is_int($pos_fintag) AND $pos_fintag>$pos_initag)
$reemplazar="si";
}
//echo "Reemplazar ".$reemplazar . " Pors initag" . $pos_initag . " Pos
fintag" . $pos_fintag ."<br>";
if($reemplazar=="si")
$str_resultado.=$i_resaltarcon.$arr_palabras_encontradas[0][$i]
.$f_resaltarcon.$arr_cadenas_a_chequear[$i+1];
else
$str_resultado.=$arr_palabras_encontradas[0][$i] .$arr_cadenas_a_chequear[$i+1];
}
return $str_resultado;
}
else
return $buscaren;
}
else
return $buscaren;
}
//I hope this helps somebody. Sorry for my english it is because I'm from argentina.