php preg_match和ereg语法的区别 [英] php preg_match and ereg syntax difference

查看:122
本文介绍了php preg_match和ereg语法的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现preg_match()和不赞成使用的ereg()的语法不同.
例如:

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

我认为

preg_match('/^<div>(.*)</div>$/', $content);

ereg('^<div>(.*)</div>$', $content);

但是我错了. preg_match()不像ereg()一样包含特殊字符.

but I was wrong. preg_match() doesn't include special characters as enter like ereg() does.

所以我开始使用这种语法:

So I started to use this syntax:

preg_match('/^<div>([^<]*)</div>$/', $content);

但与我需要的不完全相同.

but it isn't exactly the same to what I need.

有人可以建议我如何在不使用不推荐使用的功能的情况下解决此问题吗?

Can anyone suggest me how to solve this problem, without using deprecated functions?

推荐答案

对于解析HTML,我建议阅读

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

如果出于某些原因需要想要使用RegEx来执行此操作,则应该知道:

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

  • preg_match()是一个贪婪的小虫子,它将尝试吃掉您的任何东西 (.*)直到生病(这意味着递归或回溯)限制).您可以使用 U 修饰符 1 进行更改.

  • 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.

引擎希望获得单行.您可以使用 m s 修饰符 1 进行更改.

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

使用您的'而不是<字符'([^<]*) hack做得很好,因为它会强制引擎在第一个< 字符处停止,但只有 >里面没有其他标签!

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 <div> doesn't contain other tags inside!

ref: 1 PCRE模式修饰符

这篇关于php preg_match和ereg语法的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆