我正在尝试使用str_replace
来更改我的代码:
$changeli = "li id=\"head{$raiz}\" class=\"head\"";
$changeli2 = "li id=\"foot{$raiz}\" class=\"foot\"";
echo $changeli; // result li id="head-all" class="head"
echo $changeli2; // result li id="foot-all" class="foot"
$footer = str_replace($changeli, $changeli2, $footer );
它不起作用,但当我删除双引号的文本时,它的工作原理如下:
$changeli = "head{$raiz}";
$changeli2 = "foot{$raiz}";
echo $changeli; // result head-all
echo $changeli2; // result foot-all
$footer = str_replace($changeli, $changeli2, $footer );
有人可以帮助我吗?
答案 0 :(得分:1)
更好的是,尝试使用'
代替"
作为外部引用,这样您根本不必逃避它。并用你的字符串连接$ raiz。问题很可能是应该在html中转义的字符。
试试这个,这可行(see this code exactly here):
$changeli = 'li id="head'.$raiz.'" class="head"';
$changeli2 = 'li id="foot'.$raiz.'" class="foot"';
// value of $raiz is "-all" at this point (for clarity of code)
echo $changeli; // result should be li id="head-all" class="head"
echo $changeli2; // result should be li id="foot-all" class="foot"
$footer = htmlspecialchars_decode(str_replace(htmlspecialchars($changeli), htmlspecialchars($changeli2), htmlspecialchars($footer)));
答案 1 :(得分:1)
试试这个解决方案:
$changeli = 'li id="head'.$raiz.'" class="head"';
$changeli2 = 'li id="foot'.$raiz.'" class="foot"';
echo $changeli; // result li id="head-all" class="head"
echo $changeli2; // result li id="foot-all" class="foot"
$footer = '';
$footer = str_replace($changeli, $changeli2, $footer );
答案 2 :(得分:1)
根据您所说的$footer
和$raiz
的值,您很难看出出了什么问题。例如这个程序:
<?php
$footer = 'id="<li id="head-all" class="head"><p>The news of the...';
$raiz = '-all';
$changeli = "li id=\"head{$raiz}\" class=\"head\"";
$changeli2 = "li id=\"foot{$raiz}\" class=\"foot\"";
print "(BEFORE) Footer: $footer\n";
$footer = str_replace($changeli, $changeli2, $footer );
print "(AFTER) Footer: $footer\n";
生成此输出:
(BEFORE) Footer: id="<li id="head-all" class="head"><p>The news of the...
(AFTER) Footer: id="<li id="foot-all" class="foot"><p>The news of the...