PHP DOMDocument node.Value替换

时间:2015-12-08 17:59:23

标签: php dom

我在email.php中有3个标签

$output='<p>Hey Jim</p>';
$output.='<p>We appreciate you are looking at using our services!</p>';
$output.='<p>Thanks Again</p>';

我希望能够使用test.phpnewp1newp2中的文字从newp3动态替换这些p标签中的文字。

$newp1 = "Hello Mark";
$newp2 = "We have scheduled your pick-up for tomorrow morning.";
$newp3 = "Any questions gives us a call.";

$url = 'email.php';
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);

$nodes = $doc->getElementsByTagName('p');

foreach($nodes as $item ){
echo $item->nodeValue.'<br>';
}

我正在回应他们看到他们,但不知道如何实际更换它们。

1 个答案:

答案 0 :(得分:0)

在此示例中,不需要DOMDocument:

您可以在email.php中使用类似的内容:

$output='<p>##msg1##</p>';
$output.='<p>##ms2##</p>';
$output.='<p>##msg3##</p>';

并在test.php中:

$html = str_replace("##msg1##", $newp1, $html);
$html = str_replace("##msg2##", $newp2, $html);
$html = str_replace("##msg3##", $newp3, $html);