fish man
fish man

Reputation: 2720

php how to wrap html tags for each search words,

How to wrap html tags for each search words, such as highlight words. I tried my code here, but nothing changed. Where is the problem, or is there have any other good way? thanks.

<?php
$sentence = "for loops are the most complex loops in PHP. They behave like their C counterparts";//original
$search = 'php counterparts';//search words
$str = explode(' ',$search);//explode every search word
for($i=0;$i<10;$i++){
    if($str[$i]!=''){ //make sure if $str[$i] is not empty, do $newstr.
        $newstr .= '\'<b>'.$str[$i].'</b>\','; //wrap <b> tag for search words
    }
}
$newstr = substr($newstr, 0, strlen($newstr)-1);//remove last common, combine a array

$new = str_ireplace(array($str),array($newstr),$sentence);
echo $new;
?>

Upvotes: 2

Views: 5190

Answers (4)

Prisoner
Prisoner

Reputation: 27628

$str = "for loops are the most complex loops in PHP. They behave like their C counterparts";//original
$search = 'PHP counterparts';
$explode = explode(" ", $search);

foreach($explode as $word){
  $str = preg_replace("/$word/i", '<strong>$0</strong>', $str);
}

echo $str;

Upvotes: 1

symcbean
symcbean

Reputation: 48387

Why is the array bounded at 10 iterations? Why do you use a string to hold the modified words? Why the regex demarcations when your not using a regex function for the substitution?

Don't even try to fix this code - start again from scratch:

$sentence = "for loops are the most complex loops in PHP. They behave like their C counterparts";//original
$search = 'php counterparts';//search words
$srch_words = explode(' ',$search);//explode every search word
$replace_words=array();
foreach ($srch_words as $key=>$val) {
     $replace_words[$key]='<b>' . $val . '</b>';
}
$sentence=str_ireplace($srch_words, $replace_words, $sentence);

Upvotes: 3

Daren Chandisingh
Daren Chandisingh

Reputation: 2165

This would be one way:

<?php

$words = array('blue', 'red');
foreach ($words as $word) {
   $matchWords[] = "/$word/i";
}
$string = 'Red, orange, yellow, green, blue, indigo, violet. More red, bluebell';

$string = preg_replace($matchWords, '<span class="keyword">$0</span>', $string);

echo $string;

Would output:

<span class="keyword">Red</span>, orange, yellow, green, <span class="keyword">blue</span>, indigo, violet. More <span class="keyword">red</span>, <span class="keyword">blue</span>bell

You can then use CSS to apply the highlight. You might want to modify the regex to just match whole words

Upvotes: 3

Till Helge
Till Helge

Reputation: 9311

I would try it this way. It's more failesafe and less index-hassle. Might be a little slower, but that should hardly be an issue.

<?php
function highlightWords(array $words, $text) {
    $pattern = '#(';
    for ($i=0; $i<sizeof($words); ++$i) {
        $pattern .= ($i > 0) ? '|' : '';
        $pattern .= preg_quote($words[$i]);
    }
    $pattern .= ')#i';
    return preg_replace_callback($pattern, 'highlightMatch', $text);
}
function highlightMatch ($matches) {
    return sprintf('<b>%s</b>', $matches[0]);
}

Less pretty but maybe faster would be:

<?php
function highlightWords (array $words, $text) {
    $find = array();
    $replace = array();
    foreach ($words as $word) {
        $find[] = $word;
        $replace[] = sprintf('<b>%s</b>', $word);
    }
    return str_replace($find, $replace, $text);
}

Upvotes: 0

Related Questions