正则表达式PHP - 使用特定ID在div中查找事物 [英] regex php - find things in div with specific ID

查看:286
本文介绍了正则表达式PHP - 使用特定ID在div中查找事物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信这是一件容易的事情,就像我在Google上搜索和搜索一样 - 我似乎无法弄清楚这有什么问题。我在这个页面上有其他地方,我使用类似的表达式,返回我想要的。

I'm sure this is an easy one, and as much as I've googled and searched here on SO - I can't seem to figure out what is wrong with this. I have other areas on this page where I use similar expressions that return exactly what I want.

然而,我不能得到这个特定的位返回我想要的,所以也许有人可以帮助我。

However, I can't get this particular bit to return what I want, so maybe someone can help me.

我有一个特定ID的IDuser-sub-commhome - 我想从该div内取出文本。文本被标签包围,但我可以很容易地使用strip_tags来让它们消失。

I have a div with a specific ID "user-sub-commhome" - I want to pull out the text from within that div. The text is surrounded by tags but I can easily use strip_tags to get those gone. I'm using regex to try and pull the data out.

以下是我的代码:

Here is my code:

$intro = "<div id="user-sub-summary">Summary</div>
<div id="user-sub-commhome"><em>Commercial</em></div>
<div id="whatever">whatever</div>";

$regex = '#\<div id="user-sub-commhome"\>(.+?)\<\/div\>#s';
preg_match($regex, $intro, $matches);
$match = $matches[0];
echo $match;

我尝试了一些没有成功的东西,没有任何东西可以回应任何东西。所以我希望有一些力量是正则表达式更有经验的人可以提供帮助。

I've tried changing things with no success, nothing seems to work to echo anything. So I'm hoping some power that be who is much more experienced with regex can help.

推荐答案

您将 $ intro 附近的双引号改为单引号:

Your code works for me if you change the enclosing doublequotes around $intro to single quotes:

$intro = '<div id="user-sub-summary">Summary</div>
<div id="user-sub-commhome"><em>Commercial</em></div>
<div id="whatever">whatever</div>';

$regex = '#\<div id="user-sub-commhome"\>(.+?)\<\/div\>#s';
preg_match($regex, $intro, $matches);
$match = $matches[0];
echo $match;

您可能需要阅读关于正则表达式和HTML的一些着名建议

这篇关于正则表达式PHP - 使用特定ID在div中查找事物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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