问题描述
基本上,我有一个html块,我想在页面上回显,并且html中有$符号,而php认为这是一个变量,因此$ 1被视为变量而不是值,因此不会显示。
Basically I have a block of html that I want to echo to the page and the html has the $ sign in it and the php thinks it is a variable so $1 is treated as the variable not the value and is not displayed.
这里有标准答案,但没有用:
There is the standard answers here but none are working: PHP: How to get $ to print using echo
我的下一个想法是将字符串拆分为$并回声每个部分。
My next idea is to split the string at the $ and echo each part out.
这是我尝试回显和打印的代码。
Here is the code I have tried echo and print.
foreach ($rows as $rowmk) {
$s = $rowmk->longdescription;
//$s = str_replace('$', '@', $s);
$s = str_replace('$', '\$', $s);
//echo "$s" . "<br>";
print $s;
}
感谢所有帮助。
好的,我通过使用$的字符代码值来解决
OK I solved by using the character code value for $
foreach ($rows as $rowmk) {
$s = $rowmk->longdescription;
$s = str_replace('$', '$', $s);
echo $s . "<br>";
}
我认为我还是应该将其发布。
I figured I should just post it anyway.
谢谢
垫子
推荐答案
您在错误的位置进行操作。当声明双引号字符串文字(在您的情况下存储在 $ rowmk-> longdescription
中)时,便完成变量插值。完成后,您将无法执行任何操作来恢复您的 $
。
You're doing it in the wrong place. Variable interpolating is done when double quoted string literal (which in your case is stored within $rowmk->longdescription
is daclared. Once it's done, you can't really do anything to get your $
s back.
解决方案,在声明字符串时进行适当的转义。
Solution, do proper escaping, when you declare the string.
这篇关于如何用php打印$的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!