Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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
Old 04-26-2011, 03:22 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Sure, the pattern for echo is always the same: echo{\s{0,}}{stringout};?. So this should match all without any problems: #^echo\s*(.*);?$#U. The last semi colon is technically not necessary in a PHP script, so I added that to the case, though you cannot be certain if its the end of the script or not (since I have no idea what you are doing with this).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 04-26-2011, 05:33 PM   PM User | #3
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
Thanks for the reply but with that regex it gobbles up to much as the dot all (.*) is greedy as there is no ? to limit it. I had the semi colon problem being at the end of the code in the back of my mind but didn't want to complicate the matter myself, as I cannot even seem to solve this problem.
RedHouse is offline   Reply With Quote
Old 04-26-2011, 06:28 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Oh I see what you mean. I just tested it as well, and while I could get it to work with a preg_match, it would not work with a match all.
Try this instead: #^echo\s*(.*);?\s*$#msiU
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 04-27-2011, 12:25 PM   PM User | #5
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
Appreciate the effort but it still doesn't seem to match all cases.
RedHouse is offline   Reply With Quote
Old 04-27-2011, 02:36 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by RedHouse View Post
Appreciate the effort but it still doesn't seem to match all cases.
Post these cases. I matched this against all the ones you have provided using both a preg_match and preg_match_all successfully.
Running against this:
PHP Code:
<?php
$str 
= <<<STRING
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';
STRING;

$pattern '#^echo\s*(.*);?\s*$#msiU';
echo 
'ALL:';
if (
preg_match_all($pattern$str$matches))
{
    
printf("<pre>%s</pre>"print_r($matchestrue));
}

echo 
'<br />EACH:';
$a explode(PHP_EOL$str);
foreach (
$a AS $item)
{
    if (
preg_match($pattern$item$matches))
    {
         
printf("<pre>%s</pre>"print_r($matchestrue));
    }
}


?>
Produces these results, which appear to be correct for your description:
Code:
ALL:

Array
(
    [0] => Array
        (
            [0] => echo "this is some text"; 
            [1] => echo "this is some text with a ; semi colon in the middle"; 
            [2] => echo "this is some text with \" double quotes in the middle"; 
            [3] => echo 'this is some text with 'single quotes' in the middle'; 
            [4] => echo $variable; 
            [5] => echo "string" . $variable; 
            [6] => echo $variable . "string"; 
            [7] => echo $variable . 'string';
        )

    [1] => Array
        (
            [0] =>  "this is some text"
            [1] =>  "this is some text with a ; semi colon in the middle"
            [2] =>  "this is some text with \" double quotes in the middle"
            [3] =>  'this is some text with 'single quotes' in the middle'
            [4] =>  $variable
            [5] =>  "string" . $variable
            [6] =>  $variable . "string"
            [7] =>  $variable . 'string'
        )

)


EACH:

Array
(
    [0] => echo "this is some text"; 
    [1] =>  "this is some text"
)

Array
(
    [0] => echo "this is some text with a ; semi colon in the middle"; 
    [1] =>  "this is some text with a ; semi colon in the middle"
)

Array
(
    [0] => echo "this is some text with \" double quotes in the middle"; 
    [1] =>  "this is some text with \" double quotes in the middle"
)

Array
(
    [0] => echo 'this is some text with 'single quotes' in the middle'; 
    [1] =>  'this is some text with 'single quotes' in the middle'
)

Array
(
    [0] => echo $variable; 
    [1] =>  $variable
)

Array
(
    [0] => echo "string" . $variable; 
    [1] =>  "string" . $variable
)

Array
(
    [0] => echo $variable . "string"; 
    [1] =>  $variable . "string"
)

Array
(
    [0] => echo $variable . 'string';
    [1] =>  $variable . 'string'
)
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
RedHouse (05-14-2011)
Old 04-27-2011, 03:07 PM   PM User | #7
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
Apologies I thought the examples I provided would be sufficient, here are a couple of cases I found that did not match from my actual code. I added them to the bottom of the test cases.

PHP Code:
<?php 
$str 
'
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\'; 
echo "<div style=\"font-size: 20px; color: #f33; text-align: center;\">
Thanks for your message. We will be in touch shortly</div>";
value="<?php echo $telephone; ?>" 
'
;

$pattern '#^echo\s*(.*);?\s*$#msiU'
echo 
'ALL:'
if (
preg_match_all($pattern$str$matches)) 

    
printf("<pre>%s</pre>"print_r($matchestrue)); 


echo 
'<br />EACH:'
$a explode(PHP_EOL$str); 
foreach (
$a AS $item

    if (
preg_match($pattern$item$matches)) 
    { 
         
printf("<pre>%s</pre>"print_r($matchestrue)); 
    } 



?>
RedHouse is offline   Reply With Quote
Old 04-27-2011, 03:32 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Okay, I see. Its not just that there is a newline (initially I could get it to work, but when I translated it so I could view it I saw a problem). The end of the &rt; indicates a semi-colon at the end of the line, so PCRE *thinks* its all done resulting in only half of the data.
I'll get back to you on this one then, it won't be as simple as I thought.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 04-27-2011, 04:05 PM   PM User | #9
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
Ah thankyou, that makes sense, I didn't realise that the &rt; would be causing the problem. Just didn't understand.
RedHouse is offline   Reply With Quote
Old 05-12-2011, 11:15 PM   PM User | #10
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
Sorry to revive this, but did you ever make any progress on it?
RedHouse is offline   Reply With Quote
Old 05-13-2011, 12:02 AM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by RedHouse View Post
Sorry to revive this, but did you ever make any progress on it?
Nope, I got busy and forgot about it.
Did you get any further?
Fou-Lu is offline   Reply With Quote
Old 05-13-2011, 11:20 AM   PM User | #12
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
Na I got stuck so I fudged it, just used two regexes and two preg matches but no worries dude, cheers for your help and effort anyway.
RedHouse is offline   Reply With Quote
Old 05-13-2011, 02:18 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by RedHouse View Post
Na I got stuck so I fudged it, just used two regexes and two preg matches but no worries dude, cheers for your help and effort anyway.
Haha, ok then I won't bother. Quite glad actually, last I recall I was trying recursion on something that seemingly shouldn't require it.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
expressions, perl, php, regex, regular

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:21 PM.


Advertisement
Log in to turn off these ads.