PHP : couldn't replace using preg_replace()
Just I'm replacing the object tag in 开发者_C百科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?
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);
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!
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>@"
精彩评论