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?

AgentConundrum
  • 20,288
  • 6
  • 64
  • 99
A.C.Balaji
  • 1,053
  • 4
  • 13
  • 23

3 Answers3

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);
Thomas
  • 17,016
  • 4
  • 46
  • 70
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!

Community
  • 1
  • 1
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
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>@"
Álvaro González
  • 142,137
  • 41
  • 261
  • 360