Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用preg_match_all获取html标记内容_Php_Regex - Fatal编程技术网

Php 如何使用preg_match_all获取html标记内容

Php 如何使用preg_match_all获取html标记内容,php,regex,Php,Regex,我有一些html代码,其中包含以下内容: <table class="qprintable2" width="100%" cellpadding="4" cellspacing="0" border="0"> content goes here ! </table> 内容在这里! 我有这个功能来匹配里面的标签 function getTextBetweenTags($string, $tagname) { $pattern = "/<table class

我有一些html代码,其中包含以下内容:

<table class="qprintable2" width="100%" cellpadding="4" cellspacing="0" border="0">
content goes here !
</table>

内容在这里!
我有这个功能来匹配里面的标签

function getTextBetweenTags($string, $tagname)
{
  $pattern = "/<table class=\"class1\" width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\">(.*?)<\/$tagname>/"; 
  preg_match_all($pattern, $string, $matches);
  return $matches[1];
}
函数getTextBetweenTags($string,$tagname) { $pattern=“/(**?/”; preg_match_all($pattern,$string,$matches); 返回$matches[1]; } 但是它没有,所以如果你能给我一个很好的模式,我将不胜感激:(

你应该避免这种情况,但是你可以使用正则表达式,比如:

preg_match('#<table[^>]+>(.+?)</table>#ims', $str);
您应该避免这种情况,但可以使用如下正则表达式:

preg_match('#<table[^>]+>(.+?)</table>#ims', $str);

AWESOMEST REGEX HTML解析器EVAR的可能重复:AWESOMEST REGEX HTML解析器EVAR的可能重复: