php preg_match and ereg syntax difference

前端 未结 1 447
傲寒
傲寒 2021-01-23 23:29

I found that syntax of preg_match() and the deprecated ereg() is different.
For example:

I thought that

preg_match(\'/^&         


        
1条回答
  •  囚心锁ツ
    2021-01-24 00:07

    For parsing HTML I'd suggest reading this question and choosing a built in PHP extension.

    If for some reason you need or want to use RegEx to do it you should know that:

    • preg_match() is a greedy little bugger and it will try to eat your anything (.*) till it get's sick (meaning it hits recursion or backtracking limits). You change this with the U modifier1.

    • the engine expects to be fed a single line. You change this with the m or s modifiers1.

    • using your 'not a < character' ([^<]*) hack does a good job as it forces the engine to stop at the first < char, but will work only if the

      doesn't contain other tags inside!

    ref: 1 PCRE Pattern Modifiers

    0 讨论(0)
提交回复
热议问题