我之前问过为什么字符串中的 \n 没有返回换行符。
这是因为字符串需要在双引号中。(感谢@Juhana!)
现在我正在使用 GET,所以我可以更改 URL 中的文本字符串
$text = $_GET["msg"];
和网址:
http://www.mywebsite.co.uk/image.php?msg=Something\nElse
结果输出文本“Something\\nElse”
但我想要的是这样的换行符:
“Something
Else”
为什么文本仍然有这两个反斜杠?
我是否错误地认为因为“味精”在双引号中所以应该创建一个换行符?
谢谢
编辑完整的 PHP 代码
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 100); //image big enough to display two lines
// Create some colors
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
//$text = 'Testing\ntesting'; using SINGLE quotes like this results \\n
//$text = "Testing\ntesting"; using DOUBLE quotes like this results in line break
$text = $_GET["msg"];
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>