Re: regex issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



nate@xxxxxxxxxxxxxxxx wrote:
All I want to do is capture the keyword (array, break, echo, etc) and color
it.

$txt = "this is an array('test')";
$pattern = "/(array|break|echo|continue)([\(.|\s.|\;.])/";
echo preg_replace($pattern, '<font color="red">$0</font>', $txt);

This captures "array(" though and I just want "array".

That's because you're using $0 in the replacement.

The value of $0 is the entire string being matched by the regex. In this case, "array(". You only want to put the keyword submatch inside the font tag, then the rest of the matched string after it.

That wasn't a very good explanation.  Sorry, I'm tired.

Anyway, change the preg_replace line to:
echo preg_replace($pattern, '<font color="red">$1</font>$2', $txt);

--Rick

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux