Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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中从html获取文本_Php - Fatal编程技术网

在PHP中从html获取文本

在PHP中从html获取文本,php,Php,我想用PHP从Html中获取纯文本。 已尝试使用库,但在某些情况下似乎失败。 我将在html中添加标题标记、段落和div标记,只需从中返回纯文本即可 下面是我试过的代码 require_once('class.html2text.inc'); // The “source” HTML you want to convert. $html = '<div class="mozaik-inner" style="font-family:Arial, Helvetica, sans-serif;

我想用PHP从Html中获取纯文本。 已尝试使用库,但在某些情况下似乎失败。 我将在html中添加标题标记、段落和div标记,只需从中返回纯文本即可

下面是我试过的代码

require_once('class.html2text.inc');
// The “source” HTML you want to convert.
$html = '<div class="mozaik-inner" style="font-family:Arial, Helvetica, sans-serif;font-size:14px;line-height:22.4px;color:rgb(68,68,68);padding:0px 30px;margin:0px auto;width:600px;background-color:rgb(250,250,250);"><h2 style="font-family:Arial, Helvetica, sans-serif;font-size:18px;line-height:28.8px;color:#444444;padding:0px;margin:0px;">Account Details for $account_name :</h2><p style="font-family:Arial, Helvetica, sans-serif;font-size:14px;line-height:22.4px;color:#444444;padding:0px;margin:0px;">TOID: $account_to_id_c</p><p style="font-family:Arial, Helvetica, sans-serif;font-size:14px;line-height:22.4px;color:#444444;padding:0px;margin:0px;"> </p></div>';

// Instantiate a new instance of the class. Passing the string
// variable automatically loads the HTML for you.
$h2t =& new html2text($html);

// Simply call the get_text() method for the class to convert
// the HTML to the plain text. Store it into the variable.
$text = $h2t->get_text();
echo $text;

很难知道解决方案是否总是有效的,但是对于您包含的示例HTML和代码中的一般原则,这应该会有帮助

// The “source” HTML you want to convert.
$html = '<div class="mozaik-inner" style="font-family:Arial, Helvetica, sans-serif;font-size:14px;line-height:22.4px;color:rgb(68,68,68);padding:0px 30px;margin:0px auto;width:600px;background-color:rgb(250,250,250);"><h2 style="font-family:Arial, Helvetica, sans-serif;font-size:18px;line-height:28.8px;color:#444444;padding:0px;margin:0px;">Account Details for $account_name :</h2><p style="font-family:Arial, Helvetica, sans-serif;font-size:14px;line-height:22.4px;color:#444444;padding:0px;margin:0px;">TOID: $account_to_id_c</p><p style="font-family:Arial, Helvetica, sans-serif;font-size:14px;line-height:22.4px;color:#444444;padding:0px;margin:0px;"> </p></div>';

// Instantiate a new instance of the class. Passing the string
// variable automatically loads the HTML for you.
$h2t = new DOMDocument();
$h2t->loadHTML($html);

$contents = $h2t->getElementsByTagName('div');
$text = '';
foreach ( $contents[0]->childNodes as $content )   {
    $nodeType = $content->nodeName;
    if ( strtolower($nodeType[0]) == 'h' ){
        $text .= $content->textContent.PHP_EOL;
    }
    else    {
        $text .= $content->textContent;
    }
}
echo $text;
getElementsByTagName调用获取此实例中的唯一标记,因此使用[0]作为函数返回节点列表。然后在子节点上迭代

如果标记名以“h”so开头,则在文本后添加新行。您可以对此进行调整,以选择特定的标记,并对不同的内容类型执行特定的操作

如果您的内容是较大页面的一部分,您可以缩小查找内容的方式,例如使用XPath

$h2t = new DOMDocument();
$h2t->loadHTML($html);
$xp = new DOMXPath($h2t);

//$contents = $h2t->getElementsByTagName('div');
$contents = $xp->query("//div[@class='mozaik-inner']"); 

这将找到一个类为“mozaik-inner”的标记。代码的其余部分保持不变,只是一个如何查找HTML以处理更改的示例。

您需要使用dom解析器,或者您是否尝试过?演示:已经尝试了strip_标记,但我需要在div、p和heading标记上有新行。@MagnusEriksson刚刚添加了预期的输出,谢谢!如果您还需要检查包含元素的可见性,我可能会同意@andrew的建议并使用DOMDocument。
Account Details for $account_name :
TOID: $account_to_id_c 
$h2t = new DOMDocument();
$h2t->loadHTML($html);
$xp = new DOMXPath($h2t);

//$contents = $h2t->getElementsByTagName('div');
$contents = $xp->query("//div[@class='mozaik-inner']");