roflmycrisps Posted December 15, 2011 Share Posted December 15, 2011 Hi, I'm fairly new to PHP and regex, however, I am required to use it in my uni project. The project consists of an editor which a user will be able to enter php scripts into, and an output area where all script outputs will be displayed once the user has clicked the submit button. At the moment I am trying to use regex to search for variables in a script, and in this particular case maths functions. e.g. : user enters: <?php $a=5; $b=6; $c=$a+$b; echo $c; ?> I want to search for the variables in the script and their values and perform mathematical functions on the regex matches. In this case the output area would just display: 11 . I have already sorted printing echo statements, however, I am struggling on this section. Any help would be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/253194-regex-trouble/ Share on other sites More sharing options...
joe92 Posted December 15, 2011 Share Posted December 15, 2011 Hi there, what your asking for will require more than just regex I reckon. The following regex will find the variables: /([$][a-zA-Z_][a-zA-Z0-9_]*)/sm The only problem I foresee is that I was using this regex indiscriminatingly to find all variables and replace. You have three unique variables all with all unique meanings. I don't know what your current attempt at this is as you have provided no code, but I think you will probably have to run a preg_match_all using this pattern and filter out the results using array_unique on the matches to grab the unique values. Then you know where each variable is, you can go about finding what they each mean and execute the mathematical function within? Good luck! Joe Quote Link to comment https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298082 Share on other sites More sharing options...
xyph Posted December 15, 2011 Share Posted December 15, 2011 You're writing a PHP parser in RegEx? This seems rather silly - using eval or some form of pseudocode would make much more sense. Do you need to check for errors, or simply ignore them? Do you need to make sure everything is within <?php and ?> tags? Have you attempted this? Quote Link to comment https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298209 Share on other sites More sharing options...
roflmycrisps Posted December 15, 2011 Author Share Posted December 15, 2011 Hi, Thanks for your replies. Yes xyph, essentially I am to create a very basic parser. I want users to be able to enter a php script in the input box, enter submit button, and the output of this script appear in the output text area. The reason I am using regex is because my tutor has told me to, however, I did have an other idea. I was considering the idea of capturing and writing the scripts which users enter into a temporary php file on the server, and running a shell_exec function to run the file as a program and then grab the output and print it into the output area. However, im not entirely sure how to go about doing this. To answer your other two questions, yes I do need to make sure everything is within <?php and ?> tags and eventually I will need to check the scripts for errors! Any advice on how you would tackle this? Thanks in advance! Jamie Quote Link to comment https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298310 Share on other sites More sharing options...
xyph Posted December 15, 2011 Share Posted December 15, 2011 Use eval. It will execute whatever code is passed to it. Does this parser have to check for syntax errors? Or should it simply ignore statements it doesn't understand? Does it have to follow variable naming rules? Quote Link to comment https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298319 Share on other sites More sharing options...
scootstah Posted December 15, 2011 Share Posted December 15, 2011 I was considering the idea of capturing and writing the scripts which users enter into a temporary php file on the server, and running a shell_exec function to run the file as a program and then grab the output and print it into the output area. However, im not entirely sure how to go about doing this. Wow, way over-engineering it. As xyph said, use eval(). If you can't use eval() (sometimes it is disabled) then save the input to a file and then include it. Quote Link to comment https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298325 Share on other sites More sharing options...
xyph Posted December 15, 2011 Share Posted December 15, 2011 This get's extremely complex. Here's how I'd do it. I've left out proper variable naming to simplify. Match between 0-1 '$' symbols. Store that into capturing group 1 (\$?) We know if capturing group 1 isn't empty it's a variable declaration I'd then match letters, numbers, or underscores and store that into group 2 ([a-z0-9_]) That will either be the function being called or the name of the variable being set I'd then match 0-unlimited spaces \s* I'd match 0-1 '=' symbols, storing that into group 3, followed by 0-unlimited spaces (=?)\s* Then anything other than a ';' symbol 0-unlimited times into group 4, followed by a ; ([^;]*); The whole thing: (\$?)([a-z0-9_]+)\s*(=?)\s*([^;]*); Keep in mind, this won't handle the <?php and ?> tags. You'll have to first create something that grabs the data between those two tags, and applies the above RegEx to it. You will then need a function that parses the return from the RegEx. Regular Expressions are a way to do complex pattern matching in strings. RegEx isn't meant to understand or parse a document. If it must be done, you should develop an extremely strict pseudo-code. PHP's loose syntax makes it THAT MUCH harder to parse. Something like $a = 'String with ; in it'; will completely bork the RegEx above. <?php $code = '$a=5; $b=6; $c=$a+$b; echo $c;'; $expr = '/(\$?)([a-z0-9_]+)\s*(=?)\s*([^;]*);/i'; preg_match_all( $expr, $code, $results, PREG_SET_ORDER ); header( 'Content-type: text/plain' ); print_r( $results ); ?> Will output Array ( [0] => Array ( [0] => $a=5; [1] => $ [2] => a [3] => = [4] => 5 ) [1] => Array ( [0] => $b=6; [1] => $ [2] => b [3] => = [4] => 6 ) [2] => Array ( [0] => $c=$a+$b; [1] => $ [2] => c [3] => = [4] => $a+$b ) [3] => Array ( [0] => echo $c; [1] => [2] => echo [3] => [4] => $c ) ) Quote Link to comment https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298331 Share on other sites More sharing options...
roflmycrisps Posted December 16, 2011 Author Share Posted December 16, 2011 Thanks for the replies, I will give it a go! Jamie Quote Link to comment https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298348 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.