0

Possible Duplicate:
Unknown modifier in regular expression

Hey guys I'm getting an Unknown Modifier warning (fail) when trying to replace the fourth slash in a sequence with a hashmark using preg_replace, hoping you can help.

$value['path'] = preg_replace("/((.*?)(/)){4}/e", '(preg_replace("//$/", "", "\0"))#', $value['path']);

Error:

Warning: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Unknown modifier ')'
Community
  • 1
  • 1
ehime
  • 8,025
  • 14
  • 51
  • 110
  • You should `s/\//\\\//g;` Or, actually, just use another delimiter - `#`, for example, - instead of `/`. – raina77ow Oct 19 '12 at 00:39

1 Answers1

2

If you use / as a delimiter, you need to escape all / in the middle of the regex.

Personally I like to use () as delimiters, because it doesn't require any escaping and it reminds me that the first match (index 0) is the entire pattern.

Side-note: Do NOT use the e modifier. Use preg_match_callback instead.

Also, you can replace the 4th instance of a slash by doing something like this:

$parts = explode("/",$value['path'],5);
$last = array_pop($parts);
$value['path'] = implode("/",$parts)."#".$last;
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592