-1

So I have an input form where the user would enter a username, after submitting, the site would gather information based on the username, however, if there is a space within the username, the username becomes void (non-existant). How would I add a behaviour in PHP that rids of any and all spaces, so that instead of the final submitted username being john doe, it would be johndoe.


Here's my form code:

<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>

And here's the php.php file code:

//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!";
}
?>
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
JLWillliamsUK
  • 185
  • 1
  • 3
  • 17
  • you are talking about leading and trailing spaces or spaces in middle? – Alive to die - Anant May 09 '15 at 23:45
  • possible duplicate of [To strip whitespaces inside a variable in PHP](http://stackoverflow.com/questions/1279774/to-strip-whitespaces-inside-a-variable-in-php) or [How to strip all spaces out of a string in php?](http://stackoverflow.com/questions/2109325/how-to-strip-all-spaces-out-of-a-string-in-php) – Sean May 09 '15 at 23:45
  • 1
    btw, this line will fail for 2 reasons `if ( $username = $_GET["username"] )` – Funk Forty Niner May 09 '15 at 23:54
  • @Fred-ii- How so? It works fine on my end. – JLWillliamsUK May 10 '15 at 00:00
  • even with the missing semi-colon? sorry, but I find that rather hard to believe, as shown on screen. Use error reporting and you'll see it'll throw you a parse error. – Funk Forty Niner May 10 '15 at 00:03
  • @Fred-ii- Yes, even with error reporting, there seems to be nothing wrong with it. It functions correctly too. – JLWillliamsUK May 10 '15 at 00:18

2 Answers2

5

1. If you are talking about leading or trailing spaces then use trim() function.

$username =trim($username);

2 But if you are talking about middle spaces then do with preg_replace():-

$username = preg_replace('/\s+/', ' ', $username);

3 You can go with str_replace() also:-

$username = str_replace(' ','',$username);

Note:- Here $username is your user name that you are going to use.

Also you can merge first with second or third to get fully clean username without leading trailing and middle spaces. like this:-

$username = trim(preg_replace('/\s+/', ' ', $username));

There's a little typo above, but edits in stackoverflow must be at least 6 characters, so I made that non-sense words...

noyidoit
  • 28
  • 4
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
3

Use the String Replace function to replace all occurrences of a space in a string with an empty string (nothing):

$string = 'My Name';
$noSpaces = str_replace(' ', '', $string);
echo $noSpaces; // echos 'MyName'
Brad Larson
  • 170,088
  • 45
  • 397
  • 571