0

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 instead?

Thanks!

Ignacio
  • 7,947
  • 15
  • 63
  • 74
  • 2
    You shouldn't parse HTML with regex. For more info on why, read the answers to this question: http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not For the BBCode stuff, you could use the BBCode extension if you have access to PECL. http://www.php.net/manual/en/book.bbcode.php – Shabbyrobe Jul 21 '10 at 05:02
  • Thanks, there's not much documentation regarding this but I'll see around it. – Ignacio Jul 21 '10 at 13:22

1 Answers1

2

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\]%
Matchu
  • 83,922
  • 18
  • 153
  • 160
  • Thanks a billion! About the html.. I know, but it's just a quick replace for video code. Shouldn't be too problematic. – Ignacio Jul 21 '10 at 04:15
  • @Ignacio - glad to help :) I'm sure you know, but please remember to click the check mark next to the answer once SO will let you :D Thanks! – Matchu Jul 21 '10 at 04:16