使用PHP DOM希望将所有字符串显示为输出 [英] Using PHP DOM want to show all string as a output

查看:64
本文介绍了使用PHP DOM希望将所有字符串显示为输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在这里,是我在php中$ data变量中的html字符串,而该字符串
具有诸如< 140/90 mmHg OR< 130的文本/ 80 mmHg 这行不是
,这是我使用PHP DOMDocument 运行此代码时显示的,因为当行号小于&时,

Here, is my html string in $data variable in php, and that string have some text like <140/90 mmHg OR <130/80 mmHg this line not showing when i run this code using PHP DOMDocument because when coming less-than & grater-than signs its problematic.



<?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函数下面创建了将转换粗体标题的功能b $ b,并使用PHP DOMDocument 粗体显示一些单词。

Also, i have created below PHP function that will convert bold title and bold some words using PHP DOMDocument.


  1. 标题加粗:这是我的主要标题(标题并不总是相同)

  2. 粗体字:TEST ABC:,TEST XYZ:,TES T TEST:,TESTXXX :(这两个字总是相同的)

这高于2点的效果很好,只是缺少行,因为我在第一段中有上述的

this above 2 points working well just missing line as i have described above in first block.



<?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; 
} ?>

您可以在此处,也跳过该行的输出 <140/90 mmHg或<130/80 mmHg

You can run this code at here, also the output skipping this line <140/90 mmHg OR <130/80 mmHg

推荐答案

您在这里没有选择,您需要先处理字符串,然后再使用 DOMDocument :: loadHTML 。但是您不能像盲目替换的野蛮人那样做(因为在这种情况下,脚本< c>或 style 标记也将被替换)。您需要使用libxml错误来仅定位有问题的开口尖括号。您可以通过这样来完成操作(这并不快(因为您需要构建DOM树,直到错误消失,但这是正确的)

You don't have the choice here, you need to process the string before loading it with DOMDocument::loadHTML. But you can't do it like a barbarian with a blind replacement (because in this case < between script or style tags would be replaced too). You need to use the libxml errors to locate only problematic opening angle brackets. You can do it this way (it isn't fast since you need to build the DOM tree until the errors disappear but it's correct):

define('LIBXML_ERR_NAME_REQUIRED', 68);

$skeleton = '<html><head><meta charset="UTF-8"/></head><body id="root">%s</body></html>';
$htmlDoc = sprintf($skeleton, $data);

$dom = new DOMDocument;

do {
    libxml_use_internal_errors(true);
    $hasError = false;
    $dom->loadHTML($htmlDoc);
    $errors = libxml_get_errors();

    foreach ($errors as $error) {
        if ($error->code == LIBXML_ERR_NAME_REQUIRED) {
            $hasError = true;
            $htmlDoc = preg_replace('~\A(?:.*\R){' . ($error->line - 1) . '}.{' . ($error->column - 2) . '}\K<~u', '&lt;', $htmlDoc);
        }
    }
    libxml_clear_errors();
} while ($hasError);

boldFormatExplanation($dom);

foreach($dom->getElementById('root')->childNodes as $childNode) {
    echo $dom->saveHTML($childNode);
}

顺便说一句,当您使用<$时,设置DOMDocument编码属性是没有用的c $ c> DOMDocument :: loadHTML 之后,因为编码是用文档内容设置的(这是我将自己放在 $ data <周围的html框架的主要原因。 / code>和< meta charset = UTF-8 />

As an aside, it's useless to set DOMDocument encoding property when you use DOMDocument::loadHTML after, because the encoding is set with document content (this is the main reason I put myself an html skeleton around $data with <meta charset="UTF-8"/>).

关于粗体功能,您可以这样写:

About your bold function, you can write it this way:

function boldFormatExplanation(&$dom) {
    $xpath = new DOMXPath($dom);
    $title_flag = true;

    foreach($xpath->query('//text()') as $node) {
        $txt = trim($node->nodeValue);
        if (empty($txt)) continue;

        $p = $node->parentNode;
        if (preg_match("/^(TEST ABC:|TEST XYZ:|TES T TEST:|TESTXXX)\s*(.*)/s", $txt, $matches)) {
            // Put Choice in bold:
            $p->insertBefore($dom->createElement('b', $matches[1]), $node);
            $node->nodeValue = " " . $matches[2];
        } elseif ($title_flag && strtoupper($txt) === $txt) {
            // Put header in bold
            $p->replaceChild($dom->createElement('b', $txt), $node);
            $title_flag = false;
        }
    }
}

这篇关于使用PHP DOM希望将所有字符串显示为输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆