Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.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获取整个页面的内容,而不是特定的div_Php_Regex - Fatal编程技术网

Php Preg_match获取整个页面的内容,而不是特定的div

Php Preg_match获取整个页面的内容,而不是特定的div,php,regex,Php,Regex,即使我只是想得到 <div class="description">...</div> 。。。 它返回这个特定div下面的所有内容。我如何才能只获取它之间的内容 $file_string = file_get_contents(''); preg_match('/<div class="description">(.*)<\/div>/si', $file_string, $description); $description_out =

即使我只是想得到

<div class="description">...</div> 
。。。
它返回这个特定div下面的所有内容。我如何才能只获取它之间的内容

$file_string = file_get_contents('');

preg_match('/<div class="description">(.*)<\/div>/si', $file_string, $description);
$description_out = $description[1];

echo $description_out;
$file\u string=文件获取内容(“”);
preg_match('/(.*)/si',$file_string,$description);
$description_out=$description[1];
回声$description_out;
您应该使用匹配。将
(.*)
更改为
(.*)


另外,如果可能的话,尽量避免使用正则表达式来解析HTML。

这里有另一种方法,当您想要在PHP中获取/读取HTML元素时,可以使用PHP DOMDocument类来指示

<?php
// string with HTML content
$strhtml = '<!doctype html>
<html>
<head>
 <meta charset="utf-8" />
 <title>Document Title</title>
</head>
<body>
 <div id="dv1">www.MarPlo.net</div>
 <div class="description">http://www.coursesweb.net</div>
</body></html>';

// create the DOMDocument object, and load HTML from a string
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);

// gets all DIVs
$divs = $dochtml->getElementsByTagName('div');

// traverse the object with all DIVs
foreach($divs as $div) {
  // if the current $div has class="description", gets and outputs content
  if($div->hasAttribute('class') && $div->getAttribute('class') == 'description') {
    $cnt = $div->nodeValue;
    echo $cnt. '<br/>';
  }
}
?>


您可以在php.net上找到关于DOMDocument的文档。

我建议您使用类似于而不是regex的东西-它更易于使用,内存效率更高。或者,与其添加额外的抽象层,不如使用
DOMDocument
或likes@JohnBilly以马克的回答为基础,您应该使用DOM来解析HTML。