hisoka Posted April 17, 2016 Share Posted April 17, 2016 (edited) 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 : http://php.net/manual/en/function.strtoupper.php 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 April 17, 2016 by hisoka Link to comment Share on other sites More sharing options...
Ingolme Posted April 17, 2016 Share Posted April 17, 2016 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 More sharing options...
hisoka Posted April 17, 2016 Author Share Posted April 17, 2016 Hello FoxyMod I hope you got a good weekend I still do not know what the regex (.*) does ? I looked for it here for example http://www.regular-expressions.info/refcapture.html in all the site but I could not find what does (.*) do ? Link to comment Share on other sites More sharing options...
Ingolme Posted April 17, 2016 Share Posted April 17, 2016 It's a combination of (), . and *. Each of those does a separate thing. Look for the explanations of each of those three rules on the page you just linked to. Link to comment Share on other sites More sharing options...
hisoka Posted April 17, 2016 Author Share Posted April 17, 2016 (edited) 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 April 17, 2016 by hisoka Link to comment Share on other sites More sharing options...
Ingolme Posted April 17, 2016 Share Posted April 17, 2016 Don't wrap $string in quotation marks. It's a variable, you don't put variables in quotation marks. Link to comment Share on other sites More sharing options...
hisoka Posted April 17, 2016 Author Share Posted April 17, 2016 Don't wrap $string in quotation marks. It's a variable, you don't put variables in quotation marks I took the code from this page : http://www.madirish.net/402 and the $string is wrapped in quotation marks . Why is $string wrapped in quotation marks ? Link to comment Share on other sites More sharing options...
Ingolme Posted April 17, 2016 Share Posted April 17, 2016 The code on that page is wrong. The person who wrote it made a mistake. $string should not be wrapped in quotation marks. Link to comment Share on other sites More sharing options...
hisoka Posted April 18, 2016 Author Share Posted April 18, 2016 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 More sharing options...
Ingolme Posted April 18, 2016 Share Posted April 18, 2016 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 More sharing options...
hisoka Posted April 18, 2016 Author Share Posted April 18, 2016 (edited) 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 April 18, 2016 by hisoka Link to comment Share on other sites More sharing options...
justsomeguy Posted April 18, 2016 Share Posted April 18, 2016 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 More sharing options...
hisoka Posted April 18, 2016 Author Share Posted April 18, 2016 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 More sharing options...
justsomeguy Posted April 18, 2016 Share Posted April 18, 2016 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. 1 Link to comment Share on other sites More sharing options...
hisoka Posted April 26, 2016 Author Share Posted April 26, 2016 understand . You really save my life back references confused me a lot and now after your explanation I get it Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now