Forum Moderators: coopster
I have a list of keyword words in a mysql . I use this keyword to higlight the matching word in the html file.
example : PHP become <a href ="some link"> php</a>
but if it sees and existing link <a href="some.link" alt="php"> php</a>.
How do Ignore this href tags? and not do this
<a href="some.link" alt="php"> <a href> php </a> </a>.
I do not know how to ignore the <a href>...</a> tags in the html/text file when the word is inside one already,
if someone can show a sample example /code woudl be a great help.
Thanks
This is what i Have so far:based on the keyword it highlight the matching words in the html file
function highlight($input, $keyword) {
foreach( $keyword as $word ) {
/// HIGHLIGHT KEYWORD
$output = preg_replace(
"/(>¦^)([^<]+)(?=<¦$)/esx",
"'\\1' . str_replace('" . $word . "', '<B>" . $word . "</B>', '\\2')",
$input
);
$input = $output;
}
return $output;
}
Something like this?
Regular Expression – preg_replace -- Ignore text within a href [webmasterworld.com]
Thanks < I made soem minor modification to ur existing script and it work perfect.
I am jsut now gonna test for multiline links , if the script breaks or not.
Thanks again
here is the modified script :
function highlight($input, $keyword) {
foreach( $keyword as $word ) {
/// HIGHLIGHT KEYWORD
/*
$output = preg_replace(
"/(>¦^)([^<]+)(?=<¦$)/esx",
"'\\1' . str_replace('" . $word . "', '<a href=http://www.cnn.com>" . $word . "</a>', '\\2')",
$input
);
*/
$pattern = "/>([^<]*)($word)([^<]*<(?!\/a>))/ismU";
echo "pattern :$pattern <br>";
$output = preg_replace($pattern,">\\1<a href='someurl'>\\2</a>\\3",$input);
$input = $output;
}
return $output;
}