【发布时间】:2015-07-20 16:34:18
【问题描述】:
所以我有一个输入表单,用户将在其中输入用户名,提交后,网站将根据用户名收集信息,但是,如果用户名中有空格,则用户名无效(不存在) .我将如何在 PHP 中添加一个消除所有空格的行为,以便最终提交的用户名不是john doe,而是johndoe。
这是我的表单代码:
<form action="php.php" method="GET">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Enter username" required>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
这是php.php文件代码:
//I GUESS THIS IS WHERE THE TRIMMING SHOULD HAPPEN?
<?php
//error_reporting(E_ALL & ~E_NOTICE);
// Load the username from somewhere
if (
$username = $_GET["username"]
) {
//do nothing
} else {
//$username = "notch";
echo "Oops! Something went wrong!";
}
?>
【问题讨论】:
-
你说的是前后空格还是中间空格?
-
顺便说一句,这条线将失败有两个原因
if ( $username = $_GET["username"] ) -
@Fred-ii- 怎么样?对我来说效果很好。
-
即使缺少分号?抱歉,但我觉得这很难相信,如屏幕上所示。使用错误报告,你会看到它会抛出一个解析错误。