Re: regex issue

From: Date: Wed, 01 Dec 2004 07:06:11 +0000
Subject: Re: regex issue
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-203208@lists.php.net to get a copy of this message
nate@savingadvice.com 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

« previous php.general (#203208) next »