簡體   English   中英

如何在echo PHP函數中顯示此HTML字符串

[英]How to display this HTML string in echo php function

我是php的新手...我有一個將用戶名變量傳遞給php scrit的表單,這是代碼。. <form action="bottone.php" method="get"> Inserisci il tuo nome utente: <input name="username" type="text" /> <input type="submit" value="GENERA CODICE" /> </form>

我想在botton.php腳本中顯示以下HTML代碼:

<a href=www.mysite.com/$username <img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>

其中$ username是從表單傳遞的變量...如何使用echo函數呢? 謝謝

像這樣:

<?php
echo '<a href="http://www.mysite.com/'.$username.'"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>';
?>

或者像這樣:

<a href="http://www.mysite.com/<?=$username?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>

您可能要確保$ username是安全的……至少使用urlencodehtmlspecialchars或類似的東西。

* 編輯 *我假設您已經知道如何從您提到的表單中獲取$ username,但是如果您不知道,則可以這樣做:

$username = $_GET['username'];

或者,您可以借此機會使用我上面提到的那些功能(除非在回顯它之前,您將$ username用於其他目的)。

例:

$username = urlencode($_GET['username']);

或者您可以直接在回顯中執行以下操作:

<a href="http://www.mysite.com/<?=urlencode($_GET['username'])?>"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>
echo "<a href=\"http://www.mysite.com/" . htmlspecialchars($username) . "\"><img src=\"http://www.mysite.com/images/logo.jpg\" width=\"50\" height=\"50\" alt=\"La mia pagina su Mysite\"/></a>";

您可以將echo括在雙引號中,並將html屬性括在單引號中

如果您從表單獲取用戶名,請使用以下代碼。

$ username = htmlspecialchars($ _ REQUEST ['username']);

或如果您分配變量,請使用以下代碼。

$ username = htmlspecialchars(您的文本在這里...);

echo "<a href= 'http://www.mysite.com/$username'><img src='http://www.mysite.com/images/logo.jpg' width='50' height='50' alt='La mia pagina su Mysite'></a>";
echo sprintf('<a href="http://www.mysite.com/%s"><img src="http://www.mysite.com/images/logo.jpg" width="50" height="50" alt="La mia pagina su Mysite"/></a>', htmlspecialchars($username, ENT_QUOTES, 'UTF-8'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM