Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/253194-regex-trouble/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298082
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298209
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298310
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298325
Share on other sites

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
        )

)

Link to comment
https://forums.phpfreaks.com/topic/253194-regex-trouble/#findComment-1298331
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.