开发者

Replacing html tag with preg_replace

开发者 https://www.devze.com 2023-01-08 17:11 出处:网络
I\'m trying to matchtags using preg_replace. The regex used is: <video[^>]*>(.*?)</video>

I'm trying to match tags using preg_replace. The regex used is: <video[^>]*>(.*?)</video>

But I'm getting a server warning: Message: preg_replace() [function.preg-replace]: Unknown modifier ']'

Any clues on why?

Also, How could I modify the regex so it can match [video] tags ins开发者_运维知识库tead?

Thanks!


Don't forget to delimit your regex, as required in the preg_ functions. Usually we would write /regex/, but any delimiters will do.

Since your regex contains /, I'll go for %, to avoid escaping it.

%<video[^>]*>(.*?)</video>%

Of course, watch out for the perils of trying to mess with HTML via regex. There will be issues. As always.

If you want [video] instead, just replace all <> with [] - but remember to escape them, since [ and ] are significant in regular expressions!

%\[video[^\]]*\](.*?)\[/video\]%
0

精彩评论

暂无评论...
验证码 换一张
取 消