22

i'm searching for keywords in a string via a regular expression. It works fine for all keywords, exept one which contains a forward slash in it: "time/emit" .

Even using preg_quote($find,'/'), which escapes it, i still get the message:

Unknown modifier 't' in /frontend.functions.php  on line 71

If i print the find pattern, it shows /time\\/emit/ . Without preg_quote, it shows /time/emit/ and both return the same error message.

Any bit of knowledge would be useful.

4 Answers 4

45

Try to begin and end your regular expression with different sign than /

I personally use `

I've seen people using #

I think most chars are good. You can read more about it here: http://pl.php.net/manual/en/regexp.reference.delimiters.php

Like this:

 preg_match('#time/emit#', $subject);  // instead of /time/emit/

To put it another way: Your $find variable should contain rather #time/emit# than /time/emit/

Sign up to request clarification or add additional context in comments.

Comments

4

looks like you have something already escaping it..

preg_quote('time/emit') // returns time\/emit
preg_quote('time\/emit') // returns time\\/emit

as a hack you could simply do:

preg_quote(stripslashes($find)) // will return time\/emit

1 Comment

preg_quote() by default does not escape /, you have to explicitly nominate that character as the second parameter of preg_quote(). `\` is escaped by default.
0

bit of code?

the the 'regex' for that particular term should look something like '/time/emit/'. With a set of keywords there may be a more efficient method so seeing what you are doing would be good.

Comments

0

this should work:

$a="Hello////////"; 
$b=str_replace($a,"//","/");
echo $b;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.