php explode()动态字符串

时间:2018-03-27 01:23:54

标签: php html-parsing explode

我有一个动态生成的字符串

    <p style="line-height:22px;"><b>Abstract</b><br>
Aims: The purpose of this study was to investigate a possible role of serum LDH as a predictor of cancer.
</p>

此字符串中的文字是动态的。 我只想使用explode函数从这个字符串中提取文本。 我的代码到目前为止。但它只是给我空白的结果。没有错误没有文字。我是php的新手。

这是我正在处理的页面的链接。

http://test.pjnmed.com/?mno=284644

我希望摘要下面的文字。

$content = file_get_contents("https://www.ejmanager.com/index_myjournal.php?jid=" . $jid . "&mno=" . $_GET[mno]);

$abstract_text = explode('<p style="line-height:22px;"><b>Abstract</b><br>', $content);
$content = $abstract_text[1];

$abstract_text = explode('</p>', $content);
$content = $abstract_text[0];

echo $content;

1 个答案:

答案 0 :(得分:1)

Don't use regexes for parsing HTML

$previous_value = libxml_use_internal_errors(TRUE);

$string ='<p style="line-height:22px;"><b>Abstract</b><br>
Aims: The purpose of this study was to investigate a possible role of serum LDH as a predictor of cancer.
</p>';
$dom = new DOMDocument();
$dom->loadHTML($string);
$p = $dom->getElementsByTagName('p')->item(0);
$p->removeChild($p->getElementsByTagName('b')->item(0));
echo $p->textContent;

libxml_clear_errors();
libxml_use_internal_errors($previous_value);

Demo

相关问题