简体   繁体   English

如何使用Regex - preg_match获取内容部分

[英]How to take content part using Regex - preg_match

Content in the div inludes new lines, tabulators etc. and probably that's why it doesn't work: div内容包括新行,制表符等,这可能就是为什么它不起作用:

preg_match('/<div>(.*)</div>/', $i, $matches)

I tried also preg_match('/<div>(\\n*)(.*)(\\n*)</div>/', $i, $matches) 我也试过preg_match('/<div>(\\n*)(.*)(\\n*)</div>/', $i, $matches)

but still doesn't work. 但仍然无法正常工作。 Could you help with that? 你可以帮忙吗?

Use preg_match('/<div>(.*)</div>/sm', $i, $matches) . 使用preg_match('/<div>(.*)</div>/sm', $i, $matches)

For details, what the additional letters mean, read documentation . 有关详细信息,附加字母的含义,请阅读文档

Add the multiline modifier ( m ) to your regex: 将多线修改器( m )添加到正则表达式:

preg_match('/<div>(.*)</div>/m', $i, $matches);

You can read on pattern modifiers on php.net . 你可以在php.net上阅读模式修饰符。

While this should work, please don't parse HTML with regexp. 虽然这应该工作,请不要使用regexp解析HTML。 There are a lot of information to be found on this page with some searching. 有一些搜索,这个页面上有很多信息可以找到。

尝试使用sm标志:

preg_match('/<div>(.*)<\/div>/sm', $i, $matches);

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

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