0

我之前问过为什么字符串中的 \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);
?>
4

3 回答 3

1

我认为magic_quotes在配置上是 ON。\这就是为什么当有特殊字符(反斜杠)时自动添加斜杠的原因。

if(get_magic_quotes_gpc())
    $text = stripslashes($_GET['msg']);
else 
    $text = $_GET['msg'];

但是如果你要在实时应用程序中使用这些,你需要更多的验证来避免安全问题。如果您发送带有特殊字符或 html 标记的文本,最好使用 POST 方法或至少使用哈希码和 revese。你的网址:

http://url.com/image.php?msg=<?php echo base64_encode('your text\n test');>

在 image.php 中

$text = isset($_GET['msg']) ? $_GET['msg'] : 'default text';
$text = base64_decode($_GET['msg']);
于 2012-03-29T13:46:37.350 回答
0

像这样使用 PHP_EOL:

header('Content-Type: image/jpeg');
$im = imagecreatefromjpeg('000.jpg');
$text = "Hello my name is degar007".PHP_EOL."and I am glad to see you";
imagettftext($im, 10, 0, 100, 100, 0x000000, 'DejaVuSans.ttf', $text);
imageJpeg($im);
imagedestroy($im);

干杯!

于 2015-06-10T00:27:30.080 回答
-2

你试过用<br/>而不是\n??

http://php.net/nl2br

你在哪里输出文本?在 html 中?

于 2012-03-29T13:36:56.840 回答