View Single Post
Old 04-26-2011, 02:31 PM   PM User | #1
RedHouse
New Coder

 
Join Date: Mar 2009
Posts: 32
Thanks: 5
Thanked 0 Times in 0 Posts
RedHouse is an unknown quantity at this point
Question REGEX - Matching the PHP echo statement

I've been racking my head for a while now, thought I'd bring my problem here encase anyone can easily spot how to solve my problem.

I need a regex to match any PHP echo statement. i.e. it must be able to match the following examples below

PHP Code:
echo "this is some text";
echo 
"this is some text with a ; semi colon in the middle";
echo 
"this is some text with \" double quotes in the middle";
echo 
'this is some text with single quotes in the middle';
echo 
$variable;
echo 
"string" $variable;
echo 
$variable "string";
echo 
$variable 'string'
Here is what I've come up with so far.
This regex matches everything but then fails when you don't use strings in your echo statement but you use variables instead.
PHP Code:
$regex "/echo\s+(.*?[(\".*?(?<!\\\\)\")|('.*?(?<!\\\\)')])*?;/si"
This regex matches variables but then fails when theres a semi colon inside a string.
PHP Code:
$regex "/echo\s+.*?; 
Is there one regex which matches all possibilities? I can't see it.
RedHouse is offline   Reply With Quote