how to locate a php variable using Regular Expression?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
heidee
Forum Newbie
Posts: 4
Joined: Sun May 10, 2009 1:02 am

how to locate a php variable using Regular Expression?

Post by heidee »

Hi everyone. This is my first time to come here
I am working on a project. However, I feel confused when i am trying to locate a variable from a text using preg_replace

Code: Select all

$text = preg_replace('/\$([\w\s\[\]\'\"]+)/', '\1<?php echo\$\2;?>', $text);
This code successfully locate some simple variable like $test, $abc
But when i try using some array, it miss. I think the problem is [\w\s\[\]\'\"] but I don't know what is the mistake
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: how to locate a php variable using Regular Expression?

Post by Defiline »

Use 'e' modificator:

Code: Select all

/[expression]+/ie
Then you can use your own PHP code.
I recommend you to use http://php.net/preg_replace_callback
heidee
Forum Newbie
Posts: 4
Joined: Sun May 10, 2009 1:02 am

Re: how to locate a php variable using Regular Expression?

Post by heidee »

Defiline wrote:Use 'e' modificator:

Code: Select all

/[expression]+/ie
Then you can use your own PHP code.
I recommend you to use http://php.net/preg_replace_callback
Excurese me, what is the 'e' modificator? I search google and find something irrelevant. Is there any website to have a brief description of this?
Moreover, is it my expression problem in my case, or simply use 'e' modificator can fix the problem?

Thanks
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: how to locate a php variable using Regular Expression?

Post by Defiline »

Are you trying to execute a PHP code in the result of preg_replace?
If you are, then you should use 'e':
http://php.net/manual/en/reference.pcre ... ifiers.php

If you are just trying to replace:

Code: Select all

preg_replace('/[expression]/i', 'Replaced to $1', $string);
heidee
Forum Newbie
Posts: 4
Joined: Sun May 10, 2009 1:02 am

Re: how to locate a php variable using Regular Expression?

Post by heidee »

Defiline wrote:Are you trying to execute a PHP code in the result of preg_replace?
If you are, then you should use 'e':
http://php.net/manual/en/reference.pcre ... ifiers.php

If you are just trying to replace:

Code: Select all

preg_replace('/[expression]/i', 'Replaced to $1', $string);

Actually, it is a template system
I am trying to replace the variable name in an html file and display it out using <?php echo $variable ?>
However, the script at #1 post cannot match an array $var['var'] and don't know how to fix the expression

Thanks
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: how to locate a php variable using Regular Expression?

Post by Defiline »

Code: Select all

<?php
 
// This is your HTML code with variable or something
$str = 'Hello, in $123 abc is (It is correct var: $va09) hi something .. $000';
 
// Our Regular expression (variable cannot start with a number)
$text = preg_replace('/(\$[^0-9]+[a-zA-Z0-9_]*)/', '<?php echo $1; ?>', $str);
 
// Let's see the result
echo htmlspecialchars($text);
 
?>
The result is:

Code: Select all

Hello, in $123 abc is (It is correct var: <?php echo $va09; ?>) hi something .. $000
heidee
Forum Newbie
Posts: 4
Joined: Sun May 10, 2009 1:02 am

Re: how to locate a php variable using Regular Expression?

Post by heidee »

Defiline wrote:

Code: Select all

<?php
 
// This is your HTML code with variable or something
$str = 'Hello, in $123 abc is (It is correct var: $va09) hi something .. $000';
 
// Our Regular expression (variable cannot start with a number)
$text = preg_replace('/(\$[^0-9]+[a-zA-Z0-9_]*)/', '<?php echo $1; ?>', $str);
 
// Let's see the result
echo htmlspecialchars($text);
 
?>
The result is:

Code: Select all

Hello, in $123 abc is (It is correct var: <?php echo $va09; ?>) hi something .. $000
The code work fine with $va09 but if I replace it with an array like

Code: Select all

$str = 'Hello, in $123 abc is (It is correct var: $va[09]]) hi something .. $000';
It will spilt the variable to
Hello, in $123 abc is (It is correct var: <?php echo $va[09; ?>]]) hi something .. $000
Defiline
Forum Commoner
Posts: 59
Joined: Tue May 05, 2009 5:34 pm

Re: how to locate a php variable using Regular Expression?

Post by Defiline »

You can modify it yourself.
If you would like to take an array, you can write another expression.
It is not obligatory to shove anything in one expression :)

Code: Select all

<?php
 
$text = preg_replace(/*...*/);
$text = preg_replace(/*...*/);
$text = preg_replace(/*...*/);
$text = preg_replace(/*...*/);
$text = preg_replace(/*...*/);
$text = preg_replace(/*...*/);
 
?>
Post Reply