Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php以脚本格式将变量存储为字符串_Php - Fatal编程技术网

如何使用php以脚本格式将变量存储为字符串

如何使用php以脚本格式将变量存储为字符串,php,Php,我想在下面使用文本区域表单将此脚本存储为变量,但当我这样做并回显变量时,浏览器会解释代码。我不想这样,我只想将脚本“按原样”存储在一个变量中,这样我就可以使用该变量进行进一步的操作。你知道我如何在php中使用文本区域表单来实现这一点吗 <a href="https://rads.stackoverflow.com/amzn/click/com/B008EYEYBA" rel="nofollow noreferrer"><img border="0" src="http://ws

我想在下面使用文本区域表单将此脚本存储为变量,但当我这样做并回显变量时,浏览器会解释代码。我不想这样,我只想将脚本“按原样”存储在一个变量中,这样我就可以使用该变量进行进一步的操作。你知道我如何在php中使用文本区域表单来实现这一点吗

<a href="https://rads.stackoverflow.com/amzn/click/com/B008EYEYBA" rel="nofollow noreferrer"><img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=B008EYEYBA&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=mytwitterpage-20" ></a><img src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />

顺便说一句,下面是当用户将上面的脚本放入文本区域时,我想要的确切php变量

$str = "<a href="https://rads.stackoverflow.com/amzn/click/com/B008EYEYBA" rel="nofollow noreferrer"><img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=B008EYEYBA&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=mytwitterpage-20" ></a><img src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />";
$str=”“;

非常感谢您的帮助。谢谢

如果您需要以HTML格式输出值,则需要将
替换为
。最好的方法是使用
htmlspecialchars()
函数

另外,您还被正确地告知字符串中有未替换的引号(将内部引号替换为
\“
)。

这样使用

<?php
$str = '<a href="http://rads.stackoverflow.com/amzn/click/B008EYEYBA"><img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=B008EYEYBA&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=mytwitterpage-20" ></a><img src="http://www.assoc-amazon.com/e/ir?t=mytwitterpage-20&l=as2&o=1&a=B008EYEYBA" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />' ;
echo htmlentities($str);
?>


@anagnam太好了,我还将用双引号括起来的$str变量改为单引号。没有理由丢失html格式。你应该使用
$str=@codegyu007:是的,使用herdoc是一个好方法,但他的代码本身有语法错误。你是说双引号中的双引号?我建议的方法也解决了这个问题。它不使用引号来终止字符串,因此不必对每个引号进行转义。我没有提到htmlEntletes(),因为它已经得到了回答。