PHP str_replace HTML 元素的内容基于 Class
PHP str_replace Contents of HTML Element Based on Class
我有一个包含以下 HTML 元素的页面;
<p class="total"></p>
当页面加载时,我想用一行文本填充它;
'Your total is:'
因此,html 看起来像;
<p class="total">Your total is:</p>
我已经试过了,但没用;
<?php
echo str_replace(".total", "Your total is:", "");
?>
我怎样才能做到这一点?
首先,str_replace()
只是将您传递的字符串(第三个参数)中的一个文字字符串替换为另一个字符串。
示例:
$foo = str_replace('lorem', 'foo', 'lorem bar');
// $foo now contains 'foo bar'
您目前正在尝试查找字符串 .total
并将其替换为空字符串 ""
中的 Your total is:
。
PHP 没有 html 元素、css 类 或 html id 的概念。它只能看到字符串,因此您无法搜索 .total。只需添加该文本,您可能会这样做:
str_replace('<p class="total"></p>', '<p class="total">Your total is:</p>', $theStringWithHtml);
如果您想搜索特定元素,可以使用 PHP 的 DOMDocument。
我有一个包含以下 HTML 元素的页面;
<p class="total"></p>
当页面加载时,我想用一行文本填充它;
'Your total is:'
因此,html 看起来像;
<p class="total">Your total is:</p>
我已经试过了,但没用;
<?php
echo str_replace(".total", "Your total is:", "");
?>
我怎样才能做到这一点?
首先,str_replace()
只是将您传递的字符串(第三个参数)中的一个文字字符串替换为另一个字符串。
示例:
$foo = str_replace('lorem', 'foo', 'lorem bar');
// $foo now contains 'foo bar'
您目前正在尝试查找字符串 .total
并将其替换为空字符串 ""
中的 Your total is:
。
PHP 没有 html 元素、css 类 或 html id 的概念。它只能看到字符串,因此您无法搜索 .total。只需添加该文本,您可能会这样做:
str_replace('<p class="total"></p>', '<p class="total">Your total is:</p>', $theStringWithHtml);
如果您想搜索特定元素,可以使用 PHP 的 DOMDocument。