![]() |
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:
This regex matches everything but then fails when you don't use strings in your echo statement but you use variables instead. PHP Code:
PHP Code:
|
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). |
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 |
Appreciate the effort but it still doesn't seem to match all cases.
|
Quote:
Running against this: PHP Code:
Code:
ALL: |
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:
|
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. |
Ah thankyou, that makes sense, I didn't realise that the &rt; would be causing the problem. Just didn't understand.
|
Sorry to revive this, but did you ever make any progress on it?
|
Quote:
Did you get any further? |
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.
|
Quote:
|
| All times are GMT +1. The time now is 11:37 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.