所以我有一個輸入表單,用戶輸入用戶名後,網站會收集基於用戶名的信息,但是,如果用戶名中有空格,用戶名變爲無效(不存在)。我將如何添加一個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!";
}
?>
你說的是中間的前後空格或空格嗎? –
可能重複的[在PHP中刪除變量中的空格](http://stackoverflow.com/questions/1279774/to-strip-whitespaces-inside-a-variable-in-php)或[如何去除所有空格出了一個字符串在PHP?](http://stackoverflow.com/questions/2109325/how-to-strip-all-spaces-out-of-a-string-in-php) – Sean
順便說一句,這條線會失敗因爲2個原因'if( $ username = $ _GET [「username」] )' –