简体   繁体   English

在HTML标签之间获取文本并替换它们

[英]Getting Text Between HTML Tags & Replacing Them

I would like to get text between HTML tags and replacing them dynamically. 我想在HTML标记之间获取文本并动态替换它们。 Considering HTML tags might contain anything (nested HTML tags, comments, etc) I think DOM Document class is the way to go. 考虑HTML标记可能包含任何内容(嵌套的HTML标记,注释等),我认为DOM Document类是可行的方法。 However I wasn't able to find any example for my needs. 但是我无法找到满足我需求的任何例子。 I can only get the text between of specifically selected html tag. 我只能在专门选择的html标签之间获取文本。 I also couldn't find an example to replace selected text. 我也找不到替换所选文本的示例。

<?php 
// HTML OUTPUT
$html= "<p>Subject,</p>
<h1>H1 title</h1>
<h2>H2 title</h2>
<h3>H2 title</h3>";

// DESIRED OUTPUT
$newHTML "<p>My Fav. Colors;</p>
<h1>Blue</h1>
<h2>Orange</h2>
<h3>Yellow</h3>";
?>

Basically I would like to get text from HTML output dynamically (might contain nested HTML tags, comments, javascripts scripts and so on.) and replace them (replaced values will be selected from database) to create new HTML output. 基本上我想从HTML输出中动态获取文本(可能包含嵌套的HTML标记,注释,javascripts脚本等)并替换它们(将从数据库中选择替换值)以创建新的HTML输出。

What is the best and elegant way to go? 什么是最好和优雅的方式去? Is DOM Document class is the tool I need or Regex is the way to go? DOM Document类是我需要的工具还是Regex的方法?

I will be really glad if you could show me with a small piece of code to understand it clearly. 如果你能用一小段代码向我展示清楚地理解它,我将非常高兴。

PS HTML document in question might be a page on another domain. 有问题的PS HTML文档可能是另一个域上的页面。 Such as http://anotherdomain.com/page.html . 例如http://anotherdomain.com/page.html

Here is an example of DOM. 这是DOM的一个例子。

$html= "<p>Subject,</p>
<h1>H1 title</h1>
<h2>H2 title</h2>
<h3>H2 title</h3>";

$doc = new DOMDocument;
$doc->loadHTML( '<div>' . $html . '</div>');

foreach($doc->getElementsByTagName('div')->item(0)->childNodes as $node) {
    switch ($node->nodeName) {
        case "p":
            $node->nodeValue = "My Fav. Colors";
            break;
        case "h1":
            $node->nodeValue = "Blue";
            break;
        case "h2":
            $node->nodeValue = "Orange";
            break;
        case "h3":
            $node->nodeValue = "Yellow";
            break;          
    }
}
echo $doc->saveXML($doc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM