【问题标题】:php - Cannot replace html entity using str_replacephp - 无法使用 str_replace 替换 html 实体
【发布时间】:2015-12-20 08:30:45
【问题描述】:

我有以下html实体代码:

$html = "<div id="sstssfb_aaa-theme_wrapper" class="first_sstform_wrapper"> <div class="bscon sstform_wrapper"> <div class="sst_formheaderbgr">"

我想使用str_replace 将这个字符串sstssfb_aaa-theme_wrapper 替换为新字符串,如下所示:

$num = 20;

$old = '#sstssfb_aaa-theme_wrapper';
$old = str_replace("#", "", $old);

$new = 'sstssfb_aaa-theme_wrapper' . $num;

$new_html = str_replace($old, $new, $html);

但它根本不起作用并返回原始的$html 而不是修改后的。

这里会出现什么问题以及如何解决?

编辑:

在我像这样修剪 $old 变量后它可以工作:

$old = trim(str_replace("#", "", $old));

【问题讨论】:

  • 请看最后的str_replace声明...
  • 双引号 $html
  • 同意@SunilPachlangia,因为在 $html 变量上添加双引号应该可以解决问题
  • 替换按预期工作。您的原始代码有问题,您在两者之间进行了更正。 $new_html 包含被替换的令牌。
  • @arkascha 是对的,您的代码正在运行,没有看到问题。

标签: php html replace str-replace html-entities


【解决方案1】:

试试这个

$old = '#sstssfb_aaa-theme_wrapper';
$old = str_replace("#", "", $old);

$new = 'sstssfb_aaa-theme_wrapper' . $num;

$html = str_replace($old, $new, $html);

然后打印$html就会得到结果。

【讨论】:

  • 您愿意详细说明与 OP 代码的区别吗?
  • 替换后需要赋值给变量$html
  • OP 发布的原始代码的正确答案,尽管 OP 更改了其间的代码。
猜你喜欢
  • 2013-07-09
  • 1970-01-01
  • 2016-01-11
  • 2013-10-29
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多