简体   繁体   English

正则表达式从id以特定字符串开头的div中提取内容

[英]Regular expression to extract content from a div whose id starts with a specifc string

I want to extract content from a div whose id starts with a specific string, but ends with anything. 我想从id以特定字符串开头的div中提取内容,但以任何结尾。 Example: 例:

I have a div 我有一个div

<div id="content_message_56384">
  My first post.
</div>

My code: 我的代码:

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

But I'm not not getting any result. 但我没有得到任何结果。 Please help. 请帮忙。

The code should be this: 代码应该是这样的:

$string = "<div id=\"content_message_56384\">
  My first post.
</div>";
$regex = '/<div id="content_message_(?:\d+)">(.+?)<\/div>/s';
preg_match($regex, $string, $matches);
print($maches[1]);

Please, note that I'm using a non-capturing expression here (?:\\d+) because you only need what's inside the <div> . 请注意,我在这里使用非捕获表达式(?:\\d+)因为你只需要<div>

$intro='<div id="content_message_56384">
  My first post.
</div><div id="content_message_5577577IIRRIR">
  My second possssss.
</div>';
$regex = '#\<div id="content_message_+.+"\>(.+?)\<\/div\>#s';
preg_match($regex, $intro, $matches);
print_r($matches[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM