-2

I'm learning PHP language , and I faced this line in some PHP script :

function cc($re,$val){
return preg_replace( '/('. $re . ')/ei ,
'strtolower("\\1")',
$value 
);
 }

so what does this symbol mean '//1' in strtolwer function I'm not asking for regex !!! , Im asking for /1 means in php ?

  • Use this `return preg_replace_callback( '/('.preg_quote($re,'/').')/i',function($match){ return strtolower($match[1]); }, $val);` [Sandbox](http://sandbox.onlinephpfunctions.com/code/2a1ab0de50054639c433482e90b450b10ff3431c) It's about as close as you can get with modern PHP. The `e` modifier is a well known security vulnerability, hence it's removal from PHP. – ArtisticPhoenix Mar 02 '19 at 02:20

1 Answers1

2

The backslash is an escape character, it's used to escape the proceeding backslash. What that means is that it translates \\1 to \1, and \1 is a reference to the output of the preg_replace. Your code had some errors, I cleaned it up:

function cc($re,$val){
    return preg_replace( '/('.$re.')/ei' ,'strtolower("\\1")',$val);
}

Keep in mind that this won't work in newer versions of PHP, because in newer versions of PHP, the /e modifier is no longer supported, and we're encouraged to use preg_replace_callback() instead, like so:

function cc($re,$val){
    return preg_replace_callback( '/('.$re.')/i' ,function($matches){
        return strtolower($matches[1]);
    },$val);
}
Will B.
  • 14,243
  • 4
  • 56
  • 62
dearsina
  • 3,332
  • 1
  • 21
  • 26
  • thanks a lot , but I need to know what this /1 mean , ?? – drlearrn003 Mar 02 '19 at 01:16
  • @drlearrn003 `\\1` will be translated in the replacement callback string to the regular expression pattern of `\1` which is synonymous with using `$1` representing "the first group" in the pattern with parenthesis. So pattern `(\w)\w`, with a value of `AB`, using `\1` will match `A` https://3v4l.org/CT4Ib I provided an example of either and an updated method for use with `preg_replace_callback` and PHP 5.3+ – Will B. Mar 02 '19 at 01:51
  • It is important to note, using `preg_eplace` with a callback string and without the `e` modifier, will not yield the desired results. Using `cc('\w', 'AB')` the code in your answer will return `strtolower("A")strtolower("B")` instead of the desired `ab`; In addition using `preg_replace_callback('/('.$re.')i/','strtolower("\\1")',$val)` will result in an error. Example: https://3v4l.org/di9ZQ – Will B. Mar 02 '19 at 02:01
  • @fyrye Good point. I amended my answer to include a snippet using preg_replace_callback(). – dearsina Mar 02 '19 at 02:25
  • 1
    Well it may work in this case I am sure you mean `$matches[1]` and not `$matches[0]` one is the first capture group, the other is the whole match. Me just being 'nit-picky' .. lol – ArtisticPhoenix Mar 02 '19 at 02:26
  • @ArtisticPhoenix ah yes, exactly. Changed. – dearsina Mar 02 '19 at 02:27
  • It would have been OK in this case as the whole match was the capture group. But I thought I should mention it. – ArtisticPhoenix Mar 02 '19 at 02:28