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.
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
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.
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
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;
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
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; ?>"
';
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