如何在单引号PHP中显示双引号

时间:2013-11-20 22:16:36

标签: php string

我有一个PHP echo语句:

echo "stores[".$row['BarID']."] = [". $row['BarName'] . ", " . $row['Address']. ",". $row['City']. "," . $row['State']. " 0". $row['ZipCode']. "," . $row['PhoneNumber']. ",". $row['Lattitude']. ",".$row['Longitude']. "]". ";<br>";  

输出:

stores[0] = [The Ale 'N 'Wich Pub , 246 Hamilton St ,New Brunswick,NJ 08901,732-745-9496 ,40.4964198,-74.4561079];

但我会像双语这样输出:

stores[0]=["The Ale 'N 'Wich Pub", "246 Hamilton St, New Brunswick, NJ 08901", "732-745-9496 Specialty: Sport", "40.4964198", "-74.4561079"];

我已经查看了PHP网站上的PHP字符串函数手册,但仍然不明白我如何实现它。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您错过的关键字是“转义”(请参阅​​Wiki)。最简单的例子:

echo "\"";

会输出:

"

修改

基本解释是 - 如果你想在双引号终止字符串中放入双引号你必须转义它,否则你会得到语法错误。

示例:

echo "foo"bar";
         ^
         +- this terminates your string at that position so remaining bar"
            causes syntax error. 

为了避免,你需要逃避双重报价:

echo "foo\"bar";
         ^
         +- this means the NEXT character should be processed AS IS, w/o applying
            any special meaning to it, even if it normally has such. But now, it is
            stripped out of its power and it is just bare double quote.

所以你的(它是字符串的一部分,但是你应该明白这一点并自己做其余的事情):

 echo "stores[".$row['BarID']."] = [". $row['BarName'] . ", " . $row['Address'] .

应该是:

 echo "stores[".$row['BarID']."] = [\"". $row['BarName'] . "\", \"" . $row['Address']. "\"

等等。