Hello and thanks for any help, I have a long test string and the string looks like

1) 01 1 20080617 20:01 00260 0
2) 01 1 20080617 07:11 00126 0
3) 01 1 20080616 17:50 00292 0
4) 01 1 20080616 06:51 00122 0

When I want to highlight 01 then I only want to highlight a whole "string from space to space and not all the 01's that occur inbetween for example in the second column 01 should be highlighted but not 20:01 or 00126.

$string = preg_replace('/('.$keyword.')/i', "<span style=\"color: $f; background-color: $b;\">$1</span>", $string);

Optionally I think I would also like to highlight the whole line so I need help figuring out how to preg_replace the whole word and then maybe highlight a whole line from 1) 01 1 20080617 20:01 00260 0 the line is terminated with <BR>

    I'm not sure I follow (no offense, but your explanation is ambiguous).

    Here is what I think you are talking about.. let me know if I'm getting close.
    I chose some random highlight colours.. you can substitute with your own.

    $string = <<<DATA
    1) 01 1 20080617 20:01 00260 0
    2) 01 1 20080617 07:11 00126 0
    3) 01 1 20080616 17:50 00292 0
    4) 01 1 20080616 06:51 00122 0
    DATA;
    
    $string = preg_replace('#^(\S+ )(\d+ \d+ \d+)#m', "$1<span style=\"color: #FF0000; background-color: #F7A836;\">$2</span>", $string);
    $array = explode("\n", $string);
    echo '<pre>';
    print_r($array);
    echo '</pre>';
    

    I exploded this into an array to better see the results (you can omit that part).
    Where the font is red, the background is orange (if you cut and paste and run the code):

    Output:
    Array
    (
    [0] => 1) 01 1 20080617 20:01 00260 0
    [1] => 2) 01 1 20080617 07:11 00126 0
    [2] => 3) 01 1 20080616 17:50 00292 0
    [3] => 4) 01 1 20080616 06:51 00122 0
    )

      Thank you much, and I'm sorry my explaination was not very good, maybe this will be better

      My original attempt produces

      FUNCTION highlight( $String, $Needle, $f='black',$b='yellow' )
      {
      
        $string = preg_replace('/('.$Needle.')/i', "<span style=\"color: $f; background-color: $b;\">$1</span>", $String);
      RETURN $string;
      }
      

      1) 01 1 20080617 20:01 00260 0
      2) 01 1 20080617 07:11 00126 0
      3) 01 1 20080616 17:50 00292 0

      Here is what I'm trying to get with a Needle of 01 and the following string
      1) 01 1 20080617 20:01 00260 0
      2) 01 1 20080617 07:11 00126 0
      3) 01 1 20080616 17:50 00292 0

      IF I have a needle of 00260 then the following
      1) 01 1 20080617 20:01 00260 0
      2) 01 1 20080617 07:11 00126 0
      3) 01 1 20080616 17:50 00292 0

      With your code I wasn't able to get a highlight at all, I tried the following

      $string = preg_replace('#^(\S+ )(\d+ \d+ \d+)#m', "$1<span style=\"color: #FF0000; background-color: #F7A836;\">$2</span>", $String); 
      
      $string = preg_replace('#^(\S+ )('.$Needle.')#m', "$1<span style=\"color: #FF0000; background-color: #F7A836;\">$2</span>", $String);
      
      

      I'm wondering if your code is working on a space if so then I know that it's actually not a space but a tab between each column i.e. chr(9)

      so 3)chr(9)01chr(9)1chr(9)20080616chr(9)17:50chr(9)00292chr(9)0<BR>
      That's how the full string is made, so would that make a difference in your Preg?

      Thanks for any further helpπŸ™‚

        Odd.. my code isn't working for me either for some reason.. I'll have a look at it.

          Ok, I think I got it..
          First, I had a space infront of my heredoc line:
          $string = <<<DATA and DATA;

          There cannot be a space infront of <<<DATA or DATA;
          So when you cut and paste this code, please make sure there are no spaces in front of these.

          So here is the revised code.. change the value of $needle to test a different result.

          $string = <<<DATA
          1) 01 1 20080617 20:01 00260 0 
          2) 01 1 20080617 07:11 00126 0 
          3) 01 1 20080616 17:50 00292 0 
          4) 01 1 20080616 06:51 00122 0
          DATA;
          
          $needle = '01';
          $string = preg_replace('# ('.$needle.') #m', " <span style=\"color: #FF0000; background-color: #F7A836;\">$1</span> ", $string);
          $array = explode("\n", $string); 
          echo '<pre>'; 
          print_r($array); 
          echo '</pre>'; 
          

          EDIT. I noticed in your last post, in the code snippet you provided, you have $String at the end of the preg_replace function... keep a very close eye on the case sensitivity of your variables. I would recommend keeping everything in upper or lowercase, unless you are using a variable name that is two words in it.. in that case, I would recommend only capitalizing the second word.

          Example:

          $easterBunny

          This is only a preference... but make sure all your variables are correct when dealing with upper or lowercase.

            rremm2000;10890176 wrote:

            I'm wondering if your code is working on a space if so then I know that it's actually not a space but a tab between each column i.e. chr(9)

            so 3)chr(9)01chr(9)1chr(9)20080616chr(9)17:50chr(9)00292chr(9)0<BR>
            That's how the full string is made, so would that make a difference in your Preg?

            Thanks for any further helpπŸ™‚

            If you stuff is using tabs, you can simply replace any actual spaces in the preg with \s

            $string = preg_replace('#\s('.$needle.')\s#m', " <span style=\"color: #FF0000; background-color: #F7A836;\">$1</span> ", $string);
            

              That worked perfectly thanks so much for the help. πŸ™‚

              Thank you for the advice I'll set them all to lower case

                Write a Reply...