【发布时间】:2016-08-06 05:29:14
【问题描述】:
这里是我在 php 中 $data 变量中的 html 字符串,以及那个字符串 有一些像
<140/90 mmHg OR <130/80 mmHg这样的文字这行不是 显示我何时使用 PHPDOMDocument运行此代码,因为当出现小于和大于时表示它有问题。
<?php
$data = 'THE CORRECT ANSWER IS C.
<p>Choice A Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s</p>
<p></p>
<p>Choice B Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s</p>
<p>Choice D Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s</p>
<p></p>
<p>Choice E simply dummy text of the printing and typesetting industry.</p>
<p></p>
<p><br>THIS IS MY MAIN TITLE IN CAPS<br>This my sub title.</p>
<p><br>TEST ABC: Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
<p>1) It is a long established fact <140/90 mmHg OR <130/80 mmHg making it look like readable English will uncover many web sites still in their infancy.
<br><br>2) There are many variations of passages of Lorem Ipsum available. </p>
<p><br>TEST XYZ: Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
<p><br>TES T TEST: It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>
<p><br>TESTXXX: It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>';
echo boldFormatExplanation($data);
?>
另外,我在 PHP 下创建了将转换粗体标题的函数 并使用 PHP
DOMDocument加粗一些单词。
- 加粗标题:“这是我的大写标题”(标题并不总是相同)
- 加粗的单词:TEST ABC:, TEST XYZ:, TES T TEST:, TESTXXX:(这些词总是相同的)
以上 2 点运行良好,只是缺少行,就像我一样 上面在第一个块中描述。
<?php
function boldFormatExplanation($data){
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->encoding = 'utf-8';
$dom->substituteEntities = false;
$dom->preserveWhiteSpace = true;
$internalErrors = libxml_use_internal_errors(true);// Set error level
@$dom->loadHTML($data, LIBXML_HTML_NODEFDTD);// Load html
libxml_use_internal_errors($internalErrors);// Restore error level
$xpath = new DOMXPath($dom);// Dom xpath
$title_flag = true;
foreach($xpath->query('//text()') as $node) {
$txt = trim($node->nodeValue);
$p = $node->parentNode;
if (preg_match("/^\s*(TEST ABC:|TEST XYZ:|TES T TEST:|TESTXXX)(.*)$/s", $node->nodeValue, $matches)) {
// Put Choice in bold:
$p->insertBefore($dom->createElement('b', $matches[1]), $node);
$node->nodeValue = " " . trim($matches[2]);
} else
if (strtoupper($txt) === $txt && $txt !== '') {
// Put header in bold
if($title_flag == true){
$p->insertBefore($dom->createElement('b', $txt), $node);
$node->nodeValue = "";
$title_flag = false;
}
}
}
$domData = $dom->saveHTML();
$data = htmlspecialchars_decode($domData);
return $data;
} ?>
您可以在here 运行此代码,同时输出跳过此行<140/90 mmHg OR <130/80 mmHg
【问题讨论】:
标签: php regex dom preg-replace preg-match