[英]How to use PHP preg_match_all, to obtain the contents inside a custom tag(not html),
[英]How to use preg match to get the contents of this html tag?
我试图在此标签中获得所需的内容:
<p class="address">
desired content
</p>
这是我的尝试:
preg_match_all("/\<p class=\"address\">(.*)\<\/p\>/", $contents, $matches);
但是$ matches数组为空。 请帮忙。
谢谢
您可以尝试以下方法:
$contents = '<p class="address">desired content</p>';
$res = preg_match_all("/\<p class=\"address\">(.*)\<\/p\>/s", $contents, $matches);
var_dump($res);
var_dump($matches);
$ matches不为空,对吗?
.
也匹配\\n
查看修饰符 .*?
(不稳定的运算符)以防止在捕获组中匹配</p>
像这样:
<?php
$contents = '<p class="address">
desired content
</p>';
$res = preg_match_all('#<p class="address">(.*?)</p>#s', $contents, $matches);
var_dump($matches);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.