首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将html标记添加到PHP中的字符串中

问将html标记添加到PHP中的字符串中
EN

Stack Overflow用户
提问于 2018-09-04 05:27:31
回答 3查看 7.6K关注 0票数 2

我想将html标记添加到PHP中的HTML字符串中,例如:

代码语言:javascript
复制
<h2><b>Hello World</b></h2>
<p>First</p>
Second
<p>Third</p>

第二个不包含任何html元素,因此系统将向其中添加p标记,预期结果如下:

代码语言:javascript
复制
<h2><b>Hello World</b></h2>
<p>First</p>
<p>Second</p>
<p>Third</p>

尝试使用PHP简单的HTML DOM解析器,但不知道如何处理它,下面是我的想法示例:

代码语言:javascript
复制
function htmlParser($html)
{
    foreach ($html->childNodes() as $node) {
        if ($node->childNodes()) {
            htmlParser($node);
        }
        // Ideally: add p tag to node innertext if it does not wrapped with any tag
    }

    return $html;
}

但是childNode不会循环到第二个中,因为它没有封装在其中的元素,并且regex不建议处理html标记,对它有什么想法吗?

非常感谢,谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-06 09:23:50

这是一个很酷的问题,因为它促进了对DoM的思考。

我提出了一个@sideshowbarker,慷慨评论的问题HTML解析器如何处理无标记文本,这使我思考,并提高了我对DoM的知识,特别是关于文本节点的知识。

下面是一种基于DoM的查找候选文本节点并将其填充到'p‘标记的方法。有很多文本节点应该放在一边,比如空格、回车和用于格式化的行提要(“丑陋者”可能会去掉这些)。

代码语言:javascript
复制
<?php

$html = file_get_contents("nodeTest.html"); // read the test file
$dom = new domDocument;            // a new dom object
$dom->loadHTML($html);             // build the DoM
$bodyNodes = $dom->getElementsByTagName('body');  // returns DOMNodeList object
foreach($bodyNodes[0]->childNodes as $child)      // assuming 1 <body> node
{
    $text="";
    // this tests for an untagged text node that has more than non-formatting characters
    if ( ($child->nodeType == 3) && ( strlen( $text = trim($child->nodeValue)) > 0 ) )
    { // its a candidate for adding tags
        $newText = "<p>".$text."</p>";  
        echo str_replace($text,$newText,$child->nodeValue);
    }
    else
    {   // not a candidate for adding tags
        echo $dom->saveHTML($child);
    }
}     

nodeTest.html包含了这个。

代码语言:javascript
复制
<!DOCTYPE HTML> 
<html>
<body>
    <h2><b>Hello World</b></h2>
    <p>First</p>
    Second
    <p>Third</p>
    fourth
    <p>Third</p>
    <!-- comment -->
</body>
</html>

输出是这个..。我不想重复外面的标签。注意,注释和格式设置是正确处理的。

代码语言:javascript
复制
<h2><b>Hello World</b></h2>
<p>First</p>
<p>Second</p>
<p>Third</p>
<p>fourth</p>
<p>Third</p>
<!-- comment -->

显然,您需要遍历DoM,并在每个元素节点重复搜索/替换,如果您想使事情变得更通用的话。在本例中,我们仅停留在Body节点,并处理每个直接子节点。

我不能百分之百地确定代码是否是最有效的,如果我找到更好的方法,我可能会考虑更多的代码并进行更新。

票数 2
EN

Stack Overflow用户

发布于 2018-09-04 09:03:12

用一种愚蠢的方法来解决这个问题,下面是我的代码:

代码语言:javascript
复制
function addPTag($html)
{
    $contents = preg_split("/(<\/.*?>)/", $html, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    foreach ($contents as &$content) {
        if (substr($content, 0, 1) != '<') {
            $chars = preg_split("/(<)/", $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
            $chars[0] = '<p>' . $chars[0] . '</p>';
            $content = implode($chars);
        }
    }

    return implode($contents);
}

希望还有其他优雅的方式而不是这样,谢谢。

票数 1
EN

Stack Overflow用户

发布于 2018-09-04 06:40:59

你可以试试简单HTML Dom解析器

代码语言:javascript
复制
$stringHtml = 'Your received html';

$html = str_get_html(stringHtml);

//Find necessary element and edit it
$exampleText = $html->find('Your selector here', 0)->last_child()->innertext
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52159323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档