使用PHP DOM 以粗体格式更改标题和其他字符串修改
Using PHP DOM to change title in bold format and other string modifications
<?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>';
$dom = new DOMDocument();
@$dom->loadHTML($data, LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
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 X in bold:
$p->insertBefore($dom->createElement('strong', $matches[1]), $node);
$node->nodeValue = " " . trim($matches[2]);
} else if (strtoupper($txt) === $txt && $txt !== '') {
// Put header in bold
$p->insertBefore($dom->createElement('strong', $txt), $node);
$node->nodeValue = "";
}
}
$data = $dom->saveHTML();
echo $data;
我已经尝试了第 1 点,第 2 点工作正常只需要解决第 3 个问题:
- 加粗的标题:"THIS IS MY MAIN TITLE IN CAPS"(标题并不总是相同的)
- 加粗的词:TEST ABC:、TEST XYZ:、TES T TEST:、TESTXXX:(这些词总是相同的)
- 当您 运行 此代码时,有些字符串没有显示跳行(在字符串 forex 中减少和增加:<140/90 mmHg 或 <130/80 mmHg)。
echo preg_replace('/(THIS IS MY MAIN TITLE IN CAPS)/', '<strong></strong>', $data);
Preg_replace 语法:preg_replace(regex, replace, subject)
$1 将在正则表达式
的括号内显示捕获的标题
正则表达式确实可以用来处理这个问题,但通常建议通过 DOM 执行 HTML 操作。 PHP 的 DOMDocument
提供了这个。
然后您可以使用这段代码,它遍历所有文本节点并查看是否满足两个条件中的任何一个:
- 文本以预定义列表中的单词开头
- 文字全部大写
在这两种情况下,都会使用该内容创建一个新的 strong
节点,并相应地调整原始节点。
$dom = new DOMDocument();
$dom->loadHTML($data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
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 X in bold:
$p->insertBefore($dom->createElement('strong', $matches[1]), $node);
$node->nodeValue = " " . trim($matches[2]);
} else if (strtoupper($txt) === $txt && $txt !== '') {
// Put header in bold
$p->insertBefore($dom->createElement('strong', $txt), $node);
$node->nodeValue = "";
}
}
$data = $dom->saveHTML();
上查看 运行
<?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>';
$dom = new DOMDocument();
@$dom->loadHTML($data, LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
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 X in bold:
$p->insertBefore($dom->createElement('strong', $matches[1]), $node);
$node->nodeValue = " " . trim($matches[2]);
} else if (strtoupper($txt) === $txt && $txt !== '') {
// Put header in bold
$p->insertBefore($dom->createElement('strong', $txt), $node);
$node->nodeValue = "";
}
}
$data = $dom->saveHTML();
echo $data;
我已经尝试了第 1 点,第 2 点工作正常只需要解决第 3 个问题:
- 加粗的标题:"THIS IS MY MAIN TITLE IN CAPS"(标题并不总是相同的)
- 加粗的词:TEST ABC:、TEST XYZ:、TES T TEST:、TESTXXX:(这些词总是相同的)
- 当您 运行 此代码时,有些字符串没有显示跳行(在字符串 forex 中减少和增加:<140/90 mmHg 或 <130/80 mmHg)。
echo preg_replace('/(THIS IS MY MAIN TITLE IN CAPS)/', '<strong></strong>', $data);
Preg_replace 语法:preg_replace(regex, replace, subject)
$1 将在正则表达式
的括号内显示捕获的标题正则表达式确实可以用来处理这个问题,但通常建议通过 DOM 执行 HTML 操作。 PHP 的 DOMDocument
提供了这个。
然后您可以使用这段代码,它遍历所有文本节点并查看是否满足两个条件中的任何一个:
- 文本以预定义列表中的单词开头
- 文字全部大写
在这两种情况下,都会使用该内容创建一个新的 strong
节点,并相应地调整原始节点。
$dom = new DOMDocument();
$dom->loadHTML($data, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXPath($dom);
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 X in bold:
$p->insertBefore($dom->createElement('strong', $matches[1]), $node);
$node->nodeValue = " " . trim($matches[2]);
} else if (strtoupper($txt) === $txt && $txt !== '') {
// Put header in bold
$p->insertBefore($dom->createElement('strong', $txt), $node);
$node->nodeValue = "";
}
}
$data = $dom->saveHTML();
上查看 运行