Regular expression to grab form tag content doesn&

2020-07-24 04:48发布

问题:

I am trying to grab contents/tags inside form tag using preg_match_all, here is the regular expression

/<form\b[^>]*>(.*?)<\/form>/i

But i wonder, why it doesn't work! Any idea?

回答1:

By default, the . (DOT) does not match line breaks. If you enable DOT-ALL with the s modifier, it does match those chars:

/<form\b[^>]*>(.*?)<\/form>/is

Realize that you won't be able to match something like:

<form>
   ...

   <!-- </form> -->

   ...
</form>

to name just one of the possibilities.



回答2:

Don't use regular expressions to parse HTML. Use an HTML parser.