higlighting problem: shouldn't highlight one letter at a time

Discussion in 'PHP' started by gilgalbiblewheel, Jul 30, 2008.

  1. #1
    What's wrong with this:
    for($m=0; $m <  count($bigWords); $m++){
    		$strText = preg_replace("/(".$keyword[$m].")/i", "<span class=\"\" id=\"\" style=\"color:".$COLORS[$m]."; 							font-weight:bold;\">$1</span>", $strText);
    		echo "<br />";
    		echo $strText;
    	}
    PHP:
    It's highlighting one letter at a time:
    <span class="" id="" style="color:red; font-weight:bold;"></span>
    t<span class="" id="" style="color:red; font-weight:bold;"></span>
    h<span class="" id="" style="color:red; font-weight:bold;"></span>
    e<span class="" id="" style="color:red; font-weight:bold;"></span>
     <span class="" id="" style="color:red; font-weight:bold;"></span>
    w<span class="" id="" style="color:red; font-weight:bold;"></span>
    h<span class="" id="" style="color:red; font-weight:bold;"></span>
    o<span class="" id="" style="color:red; font-weight:bold;"></span>
    
     <span class="" id="" style="color:red; font-weight:bold;"></span>
    a<span class="" id="" style="color:red; font-weight:bold;"></span>
    n<span class="" id="" style="color:red; font-weight:bold;"></span>
    d<span class="" id="" style="color:red; font-weight:bold;"></span>
     <span class="" id="" style="color:red; font-weight:bold;"></span>
    b<span class="" id="" style="color:red; font-weight:bold;"></span>
    u<span class="" id="" style="color:red; font-weight:bold;"></span>
    Code (markup):

     
    gilgalbiblewheel, Jul 30, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What is the $keyword variable..? It seems like it's a string and not an array so doing $keyword[$m] would do one letter at a time from the string. Since you're doing it from count($bigWords), shouldn't you be using $bigWords[$m] in the preg_replace?
     
    zerxer, Jul 30, 2008 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    I realized when I pasted that section the $keyword wasn't mentioned before. And echo $strtext should be placed outside the for loop.
     
    gilgalbiblewheel, Jul 31, 2008 IP