Jump to content

one inside strtoupper


hisoka
 Share

Recommended Posts

In this code :

<?php
$string = "this is my lower sting";
print preg_replace('/(.*)/e', 'strtoupper("\\1")', '$string');
?>

I do not understand what does

\\1 

do inside

strtoupper 

I read here :

 

that 
strtoupper

 

takes a string as a parameter not \\1 ??

Second question : What does regex (.*) do ? I know that .* means any character, any number of repetitions but not (.*)

 

??




			
				


	Edited  by hisoka
	
	

			
		
Link to comment
Share on other sites

That "strtoupper" is not a function, it's just a string. It's not actually PHP code.

 

As for the \1, it's called a backreference. It's explained in the PHP manual for preg_replace():

http://php.net/manual/en/function.preg-replace.php

 

replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\\\" PHP string).

 

You should do more research on regular expressions if you really want to understand all of this. The information is out there if you look for it.

Link to comment
Share on other sites

After some researches , I find that the parentheses are used to group the . and * together , I find , too, that .* does the same thing as (.*) . So I deleted the parentheses and run the code again like this :

<?php
$string = "this is my lower sting";
print preg_replace('/.*/e', 'strtoupper("\\1")', '$string');
?>

but nothing happened . If (.*) does the same thing as .* so why I got a blank page and nothing happened ?

Edited by hisoka
Link to comment
Share on other sites

The code on that page is wrong. The person who wrote it made a mistake

 

 

but this code :

<?php
$string = "this is my lower sting";
print preg_replace('/(.*)/e', 'strtoupper("\\1")', '$string');
?>

gives exactly the same result as this code :

<?php
$string = "this is my lower sting";
print preg_replace('/(.*)/e', 'strtoupper("\\1")', $string);
?>

which is : THIS IS MY LOWER STING

 

So if

'$string'

is wrong why it did not engender any error and gave a normal result as $string without quotation marks ? that is what I wanted to know




			
		
Link to comment
Share on other sites

The first block of code you provided would actually output $STRING, the second one would output THIS IS MY LOWER STING

 

There would be no error message, it just would not work correctly.

Link to comment
Share on other sites

The first block of code you provided would actually output $STRING

 

No my friend the first block , I provided , outputs THIS IS MY LOWER STING and not $STRING . Try to run it yourself and you will see that it outputs THIS IS MY LOWER STING :)

Edited by hisoka
Link to comment
Share on other sites

I believe that is a side-effect of using the /e modifier, although I don't see it documented. It's also worth pointing out that the /e modifier is deprecated in PHP 5.5 and removed in PHP 7, as it can be a security risk. The preg_replace_callback function is the safe alternative.

 

edit: it's because this is what gets eval'd:

 

strtoupper("$string")
Link to comment
Share on other sites

I am still confused concerning this:

 

After some researches , I find that the parentheses are used to group the . and * together , I find , too, that .* does the same thing as (.*) . So I deleted the parentheses and run the code again like this :

<?php
$string = "this is my lower sting";
print preg_replace('/.*/e', 'strtoupper("\\1")', '$string');
?>

but nothing happened . If (.*) does the same thing as .* so why I got a blank page and nothing happened ?

Link to comment
Share on other sites

If (.*) does the same thing as .* so why I got a blank page and nothing happened ?

It doesn't do the same thing. If it did the same thing, why would we need another way to do it? The parentheses indicate that it should match that part. That's what the \1 reference is referring to, the match of subpattern #1, which is inside those parentheses. If you had a pattern like this:

 

/(\d)(\w)/
Then using \1 as a back-reference would refer to the \d match, and using \2 would refer to the \w match. The parentheses create a subpattern.
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...