需要帮助。我无法取代'&'与'&'在PHP中使用Preg_replace。但是,如果我在xml文件上手动(编辑)它,它可以正常工作。
以下是样本:
$XMLCharactersPreg = "/[&<>\"\'()^*@+]/";
$XMLPregReplace = "&";
$d_Description = "50% offer & 20% further reduction for member";
if (preg_match($XMLCharactersPreg, $d_Description)) {
echo "A match was found.";
$XMLDealDescription = preg_replace($XMLCharactersPreg , $XMLPregReplace, $d_Description);
echo "$XMLDealDescription <br / >";
} else {
echo "A match was not found.";
}
感谢。
答案 0 :(得分:0)
为什么不使用htmlspecialchars?
$XMLDealDescription = htmlspecialchars($d_Description, ENT_QUOTES);
然后代码变为:
$d_Description = "50% offer & 20% further reduction for member";
$XMLDealDescription = htmlspecialchars($d_Description, ENT_QUOTES);
echo "$XMLDealDescription <br / >";
此代码工作正常 - 输出为:
> g:\Program Files\PHP>php Test\test.php.txt
50% offer & 20% further reduction for member <br / >
您获得的任何其他错误(例如XML格式良好)可能是由于代码中的其他位置出现错误。 (此外,&lt; br /&gt;标记似乎在/和&gt;之间有一个空格,这是不需要的。)
答案 1 :(得分:0)
您是否可以在浏览器中查看结果,该结果会将&
显示为&
?因此,源代码包含&
,但您只能在浏览器中看到&
。