2013-12-12 31 views
0

我想在textarea中使用特殊文本作爲具有值的變量。PHP - 如何使用文本%從文本區域到變量

例如:

在文本區域的文本%Url%等於http://example.com/

它更可能是這樣的:

如果在PHP中,它會像這樣:

$Url = 'http://example.com'; 
echo 'This is an example'.$Url.'of a website'; 

輸出HTML:This is an example http://example.com of a website

我想要做的是使textarea像上面的代碼一樣工作。

textarea的

%Url% = http://example.com/ 

<textarea name='test'>This is an example %Url% of a website</textarea> 

fiddle

所以當textarea的值被髮送到服務器,我想PHP讀取它:

This is an example http://example.com/ of a website 

,或者$_POST['test'](文字區域名稱爲測試)在submi後回顯那麼它會輸出:

This is an example http://example.com/ of a website 

如何實現它?我看到一個應用程序能夠做到這一點!

+2

沼澤標準「PHP 101」的代碼,你可以都颳起了自己:'這是一個例子的網站 –

回答

2

我會用str_replace

echo "<textarea name='test'>" . str_replace("%Url%", "http://example.com", $url) . "</textarea>"; 
0

如果發佈的URL,你的PHP那麼你可以收集它爲正常(如馬克·乙說)接近這個。但是,如果您將其顯示回屏幕,請記住將其轉義以防止跨站點腳本(XXS)攻擊。使用htmlentities()轉義任何傳入的HTML。

所以一些簡單,因爲這應該這樣做:

$url = htmlentities($_POST['Url']); 
echo "This is an example {$url} of a website"; 
相關問題