Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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_String - Fatal编程技术网

Php 按原样创建包含混合引号的字符串变量

Php 按原样创建包含混合引号的字符串变量,php,string,Php,String,如何获取包含大量双引号和单引号的字符串: curl -A 'Curl/2.5' -d '{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}' curl-A'curl/2.5'-d'{“key”:“$api_key”,“message”:{“html”:“helloworld”} 并创建一个字符串变量,例如: $curl_command = "curl -A 'C

如何获取包含大量双引号和单引号的字符串:

curl -A 'Curl/2.5' -d '{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}'
curl-A'curl/2.5'-d'{“key”:“$api_key”,“message”:{“html”:“helloworld”}
并创建一个字符串变量,例如:

$curl_command = "curl -A 'Curl/2.5' -d '{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}'"
$curl\u command=“curl-A'curl/2.5'-d'{“key”:“$api\u key”,“message”:{“html”:“Hello World”}”

在不返回“意外”错误的情况下?我希望避免更改原始字符串。

您可以使用herdoc语法,轻松地转义所有引号和双引号

下面是它的语法。


注意不要在heredoc标识符前留下空格。

您可以使用heredoc语法,轻松地转义所有引号和双引号

下面是它的语法。


注意不要在heredoc标识符前留下空格。

使用
heredoc
语法

<?php
$api_key='xx2233';
$content=<<<EOD
curl -A 'Curl/2.5' -d '{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}'
EOD;

echo $curlCommand=$content;

使用
herdoc
语法

<?php
$api_key='xx2233';
$content=<<<EOD
curl -A 'Curl/2.5' -d '{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}'
EOD;

echo $curlCommand=$content;
您可以使用herdoc:

$string = <<<someStringYouWillAlsoNeedToStop
curl -A 'Curl/2.5' -d '{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}'
someStringYouWillAlsoNeedToStop; // this ends your string
$string=您可以使用以下代码:

$string = <<<someStringYouWillAlsoNeedToStop
curl -A 'Curl/2.5' -d '{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}'
someStringYouWillAlsoNeedToStop; // this ends your string
$string=使用函数来保证完全转义:

$arg = escapeshellarg('{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}');
$curl_command = "curl -A 'Curl/2.5' -d '.$arg;
$arg=escapeshellarg(“{”key:“$api_key”,“message:{”html:“Hello World”}”);
$curl_command=“curl-A'curl/2.5'-d”。$arg;
使用该功能确保完全转义:

$arg = escapeshellarg('{"key":"$api_key","message":{"html":"<p><h1>Hello World</h1><\/p>"}');
$curl_command = "curl -A 'Curl/2.5' -d '.$arg;
$arg=escapeshellarg(“{”key:“$api_key”,“message:{”html:“Hello World”}”);
$curl_command=“curl-A'curl/2.5'-d”。$arg;

我想在我的php文件中使用json部分中的php$变量对字符串进行硬编码。我想在我的php文件中使用json部分中的php$变量对字符串进行硬编码。EOD和EOT之间有什么区别?@JMC看到我的答案,你就会明白…没有区别。无论你使用什么。你可以n使用
EOD
EOT
blahblah
,或者甚至
一些字符串。你也可以使用EOD
谢谢,我现在知道了。他们在手册中在EOD和EOT之间切换,使其看起来有意义。EOD和EOT之间有什么区别?JMC看到我的答案,你就会明白……没有区别。不管是什么在你使用时。你可以使用
EOD
EOT
blahblah
,甚至
一些字符串。谢谢我现在知道了。他们在手册中在EOD和EOT之间切换,使它看起来有意义。