如何使使用单引号声明的字符串,评估 \n
就像使用双引号声明一样?
例如:
echo 'Line1\nLine2'; // Does not split.
echo "Line1\nLine2"; // It splits.
$s = 'A string declared using \n single quotes which I can\'t change...';
echo $s // I need this to have the split at \n
如何使使用单引号声明的字符串,评估 \n
就像使用双引号声明一样?
例如:
echo 'Line1\nLine2'; // Does not split.
echo "Line1\nLine2"; // It splits.
$s = 'A string declared using \n single quotes which I can\'t change...';
echo $s // I need this to have the split at \n
str_replace
函数将它们替换为实际的换行符即可:$s = str_replace('\n', "\n", $s);
如果您要将其显示为HTML,请注意还需要运行nl2br
(如果您使用的是模板引擎,则可能已经为您完成此操作)。
首先你需要修复你的字符串。用 \'
代替 '
,然后你需要使用 str_replace()
$s = 'A string declared using \n single quotes which I can\'t change...';
$s= str_replace('\n', "\n", $s);
你能使用str_replace()
函数替换'
中的\'
吗?
$s = 'A string declared using \n single quotes which I can\'t change...';
$s= str_replace('\n', "\n", $s);
nl2br($s);