2012-03-29 133 views
0

我以前問過爲什麼\ n在一個字符串中沒有返回換行符。imagettftext \ n換行符第2部分

這是因爲字符串需要在雙引號中。 (!感謝@Juhana)

現在我使用GET這樣我就可以在URL

$text = $_GET["msg"]; 

和URL更改文本字符串:

http://www.mywebsite.co.uk/image.php?msg=Something\nElse 

結果在輸出文本「的東西\ \ nElse」
但我要的是一個換行符是這樣的:
‘一些
否則’

爲什麼文本仍然有這兩個反斜槓?
我認爲這是錯誤的,因爲「味精」在雙引號應該創建一個換行符?

感謝

EDIT完整的PHP代碼

<?php 
// Set the content-type 

頭( '內容 - 類型:圖像/ 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); 
?> 
+0

你試過用'
'而不是'\ n'??? – Ace 2012-03-29 13:32:01

+0

是的,它只是顯示
就好像這就是我想讓圖片中的文字說的那樣。 – mattneedshelp 2012-03-29 13:35:25

+0

請發佈完整的PHP代碼 – Ace 2012-03-29 13:38:33

回答

1

我認爲magic_quotes在配置上是ON。這就是爲什麼當有特殊字符如\(反斜槓)時自動添加斜線。

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

但是,如果你要在現場應用中使用這些,你需要避免 安全問題的詳細驗證。如果您發送帶有特殊字符或html標籤的文本更好使用POST方法或至少哈希代碼和崇敬。您的網址:

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']); 
+0

非常有趣!現在它使用單斜槓'\ n'輸出文本,但圖像在圖像中顯示此文本而不是創建換行符。任何想法爲什麼? – mattneedshelp 2012-03-29 13:55:04

+0

更新,請檢查 – safarov 2012-03-29 14:00:53

+0

我不明白在哪裏把新的代碼?我還需要那個'魔術引號'的代碼嗎?它似乎編碼我的網址,但顯示圖像編碼,而不是解碼。 – mattneedshelp 2012-03-29 14:12:57

-2

做ütryed使用<br/>代替\n

http://php.net/nl2br

哪兒來ü輸出的文本?在HTML?

g.r.

+0

'nl2br'顯示html標記'
'代替'\ n'。我想知道如何編寫這個URL,這樣'\ n'就可以工作,不會顯示兩個斜槓'\\ n' – mattneedshelp 2012-03-29 13:42:29

+0

想法是你可以在GET之後使用nl2b。 你也可以做一個簡單的preg_replace http://php.net/manual/de/function.preg-replace.php 所以你可以搜索'\\ n'並用'\ n'代替它。 – Ace 2012-03-29 20:20:54

+0

@Ace需要? – mattneedshelp 2012-03-30 08:26:26

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); 

喝彩!