tags:

views:

25

answers:

2
preg_replace("/(/s|^)(php|ajax|c\+\+|javascript|c#)(/s|$)/i", '$1#$2$3', $somevar);

It's meant to turn, for example, PHP into #PHP.

Warning: preg_replace(): Unknown modifier '|'

HALP

+2  A: 

The first character in the regular expression is the delimiter. If you need to use this inside your regular expression then you need to escape it:

"/(\/s|^)...
   ^

Or alternatively, choose another delimiter that isn't used anywhere in your regular expression so that you don't need to escape:

"~(/s|^)...(/s|$)~i"

I prefer to do the latter as it makes the regular expression more readable.

(Although as NullUserException points out, the actual error is that you should have used a backslash instead of a slash).

Mark Byers
+2  A: 

It's because you are using the forward slash (/) as your delimiter. When the regex engine gets to /s (3rd character) it thinks the regex is over and the rest of it are modifiers. But no such modifier (|) exists, thus the error.

Next time, you can either:

  • Change your delimiters to something you won't use in your regex, ie:

    preg_replace("!(/s|^)(php|ajax|c\+\+|javascript|c#)(/s|$)!i", '$1#$2$3', $somevar);

  • Or escape those characters with a backslash, ie: "/something\/else/"*

I also suspect you didn't intend to use /s, but the escape character \s that matches whitespace characters.

NullUserException
Thank you NullUserException. I meant to do \s... I must have just forgotten it's supposed to be around that way. This was a pretty stupid and simple mistake. Thanks.
Numbface
+1: You're probably right that he intended to write \s not /s.
Mark Byers