Problem with Regex in PHP

3

I have the following function that is used to take special characters from a string:

function removeSpecialChars($string){
  //List (Array) of special chars
  $pattern = array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/","/(ç)/","/(Ç)/","/(\'|\"|\^|\~|\;|\:|\°|\?|\&|\*|\+|\@|\#|\$|\%|\!|\|\/|\(|\)|\||\=|\.|\,)/");

  //List (Array) of letters
  $replacement = array('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'n', 'N', 'c', 'C', '');

  return preg_replace($pattern , $replacement, $string);
}

It works fine, the only problem is that this function can not replace the forward slash, whether it is inverted ( \ ) or normal ( / ), and the dollar sign ( $ ), but if I put this regular expression on some website to test, such as I placed that link works normally.

Does anyone know why it does not work in PHP?

    
asked by anonymous 11.12.2018 / 14:10

2 answers

4

This happens because within a string (between quotation marks), the backslash should be escaped and written as \ (as described in documentation ).

Therefore, \/ should be written as \/ , and \ should be written as \\ , and so on. Another detail is that the quoted character itself ( " ) must also be escaped and written as \" . That is, within the string, the \" excerpt of the expression should be written as \\" .

Then your expression would look like this:

function removeSpecialChars($string){
  //List (Array) of special chars
  $pattern = array("/(á|à|ã|â|ä)/","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","/(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/","/(ç)/","/(Ç)/","/(\'|\\"|\^|\~|\;|\:|\°|\?|\&|\*|\+|\@|\#|\$|\%|\!|\\|\/|\(|\)|\||\=|\.|\,)/");

  //List (Array) of letters
  $replacement = array('a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'n', 'N', 'c', 'C', '');

  return preg_replace($pattern , $replacement, $string);
}

echo removeSpecialChars("áçõ/\?&"); // aco

On the site you tested, you did not need to escape \ because there the regex is not inside a PHP string.

    
11.12.2018 / 14:36
4

Simplest you put everything inside% character brackets , or character set brackets, and of course, escaping the slashes and double quotation marks:

"/['\"^~;:°?&*+@#$%!\/()|=.,\\]/"
    ↑               ↑       ↑↑
 escape          escape   escape

Now I've missed other characters, such as the [] brackets and the [] braces, for example. If you include them, you need to escape the brackets too:

"/[{}\[\]'\"^~;:°?&*+@#$%!\/()|=.,\\]/"
     ↑ ↑  ↑               ↑       ↑↑
    escapes            escape   escape
    
11.12.2018 / 14:54