Page 1 of 1
how to locate a php variable using Regular Expression?
Posted: Sun May 10, 2009 1:07 am
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
Re: how to locate a php variable using Regular Expression?
Posted: Sun May 10, 2009 5:00 am
by Defiline
Use 'e' modificator:
Then you can use your own PHP code.
I recommend you to use
http://php.net/preg_replace_callback
Re: how to locate a php variable using Regular Expression?
Posted: Sun May 10, 2009 5:35 am
by heidee
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
Re: how to locate a php variable using Regular Expression?
Posted: Sun May 10, 2009 5:50 am
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);
Re: how to locate a php variable using Regular Expression?
Posted: Sun May 10, 2009 6:09 am
by heidee
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
Re: how to locate a php variable using Regular Expression?
Posted: Sun May 10, 2009 6:26 am
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
Re: how to locate a php variable using Regular Expression?
Posted: Sun May 10, 2009 6:45 am
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
Re: how to locate a php variable using Regular Expression?
Posted: Sun May 10, 2009 7:03 am
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(/*...*/);
?>