Author Topic: Search and replace outside <> tags  (Read 3122 times)

0 Members and 1 Guest are viewing this topic.

Offline razedkTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Search and replace outside <> tags
« on: June 30, 2006, 03:56:31 PM »
Hi,

I want to make a search and all normal text, ie outside HTML tags

Example 1:

Hello, I want to hightlight <a href="mailto:raze@mail.com">raze@raze.com</a>

should become

Hello, I want to hightlight <a href="mailto:raze@mail.com"><b style="color:blue;"raze</b>@mail.com</a>

Example 2:

Hello, my name is raze

should become

Hello, my name is <b style="color:blue;"raze</b>

Right now I have the following code, but it does not work with example 1, because it replaces raze inside the <a href> tag. How can I avoid that ?

$word = "raze";
$highlighted_line = preg_replace( "'($word)'si" ,  "<b style=\"color:blue;\">\\1</b>" , $line);





Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: Search and replace outside <> tags
« Reply #1 on: June 30, 2006, 04:16:00 PM »
Code: [Select]
<?php

$tests = array(
'<a href="mailto:Bob@Bob.com">E-mail</a> Bob.',
'<a href="mailto:Bob@Bob.com">Bob</a>',
'Bob',
'<a href="mailto:Sue@Sue.com">Sue</a>',
);

### For each test...
foreach ($tests as $test) {
echo '<hr /><b>Analyzing: 'htmlentities($test), '</b><br />';
### ...separate the tags from the text.
$pieces =
preg_split(
'/(<.+?>
)/',
$test,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
### For each piece...
foreach ($pieces as &$piece) {
### ...make the substitution on the non-tag pieces.
if (strpos($piece, '<') === FALSE) {
echo $piece, '<br>';
$piece = str_replace('Bob', 'Bill', $piece);
}
### ...and ignore the tag pieces.
else {
echo 'Skipping ', htmlentities($piece), '<br>';
}
}
### Now put them back together...
$string = implode('', $pieces);
echo '<b>Final: ', htmlentities($string), '</b><br>';
}

?>
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Offline razedkTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Re: Search and replace outside <> tags
« Reply #2 on: June 30, 2006, 05:37:04 PM »
Hurray, preg_split did the work... here is the complete code

Code: [Select]

function highlight_words ($text, $searchdata, $bold) {

  $word_array = explode(" ", $searchdata);

  $text_pieces = preg_split("'(<.+?>)'", $text, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

  $i = 0;

  foreach ($text_pieces as $piece) {

    if (strpos($piece, '<') === FALSE) {

      foreach ($word_array as $word) {
        if ($bold == 1) {

          $piece = preg_replace( "'($word)'si" , "<b style=\"color:blue;\">\\1</b>" , $piece);

        } else {
          $piece = preg_replace( "'($word)'si" , "<span style=\"color:blue;\">\\1</span>" , $piece);
        }
      }  //end foreach $word_array


    }

    $new_text_pieces[$i] = $piece;
    $i++;

  } //end foreach $text_pieces

  $text = implode('', $new_text_pieces);
  return $text;
}