如何显示回声PHP函数
问题描述:
这个HTML字符串我在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>
如何显示回声PHP函数
我想在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>
其中$用户名是从表单传递的变量...我怎么能做到这一点的带有回声功能? 感谢
答
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>";
答
这样的:
<?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>
您可能希望确保$的用户名是安全的,但...至少使用urlencode, htmlspecialchars,或类似的东西。
* 编辑 * 我假设你已经知道如何从你所提到的形式$的用户名,但如果你没有,你只是做:
$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>
答
您可以用双引号引起的回声和如果u得到的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'));
antislash疯狂:) – 2012-03-12 21:11:29