如何在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是安全的。。。至少使用urlencode、htmlspecialchar或类似的东西。

*编辑*我以为你已经知道如何从你提到的表格中获得$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=htmlspecialchar($_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'));