phpregexbackslash

Regex pattern containing backlashes causes failure in preg_replace()


So as far as I know, I have done this exactly as prescribed I should do, but PCRE makes absolutely no sense to me at all, so I am going to ask you what is wrong with this code and why does it say:

missing terminating ] for character class at offset 10 in

  $UrlTeams = preg_replace(
    array(
        "/[\/\\\40][\\]?/",
        "/[\.%]/",
        "/\&/"
    ),
    array(
        "/\-/",
        "",
        "\&amp\;"
    ),
    $UrlTeams
  );

Solution

  • Try:

    $UrlTeams = preg_replace(
        array(
            "#[/\40\\\\][\\\\]?#",
            "#[.%]#",
            "#[&]#"
        ),
        array(
            "-",
            "",
            "&"
        ),
        $UrlTeams
      );