2

Just I'm replacing the object tag in the given string

$matches = preg_replace("/<object(.+?)</object>/","replacing string",$str);

but it is showing the error as

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'o'

What went wrong?

3 Answers 3

1

The slash in </object> has to be quoted: <\/object>, or else it is interpreted as the end of your regex since you're delimiting it with slashes. The whole line should read:

$matches = preg_replace("/<object(.+?)<\\/object>/","replacing string",$str);
Sign up to request clarification or add additional context in comments.

Comments

0

In your regex the forward slash is the regex delimiter. As you are dealing with tags, better use another delimiter (instead of escaping it with a backslash):

$matches = preg_replace("#<object(.+?)</object>#", "replacing string", $str);

There are other delimiteres, too. You can use any non-alphanumeric, non-backslash, non-whitespace character. However, certain delimiters should not be used under any circumstances: |, +, * and parentheses/brackets for example as they are often used in the regular expressions and would just confuse people and make them hate you.

Btw, using regular expressions for HTML is a Bad Thing!

Comments

0

The first character is taken as delimiter char to separate the expression from the flags. Thus this:

"/[a-z]+/i"

... is internally split into this:

- Pattern: [a-z]+
- Flags: i

So this:

"/<object(.+?)</object>/"

... is not a valid regexp. Try this:

"@<object(.+?)</object>@"

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.