我想使用php将一个单词的所有出现替换为另一个单词。我希望我的HTML代码能够反映出我所做的相应更改。
<?php
$strhtml = '<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>time</title>
</head>
<body>
<p id="post">Hello, How are you</p>
</body>
</html>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById('post');
$c = $elm->nodeValue;
$d= date("H");
if ($d<11)
str_replace("Hello","Good Morning",$c)
?>
答案 0 :(得分:1)
您可以使用$dochtml->saveHTML()
函数将DOMDocument对象解析为html内容。这段代码对我有用。
<?php
$strhtml = '<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>time</title>
</head>
<body>
<p id="post">Hello, How are you</p>
</body>
</html>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById('post');
$c = $elm->nodeValue;
$d= date("H");
if ($d<11){
$c = str_replace("Hello","Good Morning",$c);
}
$dochtml->getElementById('post')->nodeValue = $c;
echo $dochtml->saveHTML();
?>