Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - make sure string has no whitespace

Tags:

php

How can I check if a PHP string contains any white space? I want to check if white space is in there, and then echo back an error message if true

if(strlen($username) == whitespace ){                  echo "<center>Your username must not contain any whitespace</center>"; 
like image 529
user547794 Avatar asked Nov 24 '11 03:11

user547794


People also ask

How do I check if a string contains only spaces in PHP?

PHP | ctype_space() Function A ctype_space() function in PHP is used to check whether each and every character of a string is whitespace character or not. It returns True if the all characters are white space, else returns False.

How do you check if a string contains a space?

Use the test() method to check if a string contains whitespace, e.g. /\s/. test(str) . The test method will return true if the string contains at least one whitespace character and false otherwise.

Does PHP ignore whitespace?

Whitespaces are generally ignored in PHP syntax, but there are several places where you cannot put them without affecting the sense (and the result).

How do I trim a space in PHP?

The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.


1 Answers

if ( preg_match('/\s/',$username) ) .... 
like image 180
bensiu Avatar answered Sep 22 '22 00:09

bensiu