- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP make sure string has no whitespace?
To check whether a string has no whitespace, use the preg_match() in PHP.
The syntax is as follows
preg_match('/\s/',$yourVariableName);
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $name="John Smith"; if ( preg_match('/\s/',$name) ){ echo "The name (",$name,") has the space"; } else { echo "The Name (",$name,") has not the space"; } ?> </body> </html>
Output
This will produce the following output
The name (John Smith) has the space
- Related Articles
- Remove whitespace in Java string.
- Remove extra whitespace from the beginning PHP?
- PHP – Make an upper case string using mb_strtoupper()
- PHP – Make a lower case string using mb_strtolower()
- Converting whitespace string to url in JavaScript
- How to split string on whitespace in Python?
- PHP program to check if a string has a special character
- How to remove all whitespace from String in Java?
- Find specific records which has whitespace on the second place in MySQL
- How to remove all trailing whitespace of string in Python?
- How to remove all leading whitespace in string in Python?
- Print the string after the specified character has occurred given no. of times in C Program
- How do you make a string in PHP with a backslash in it?
- The Natural number 1 has no predecessor?
- Know Sure Thing -Definition, Formula & Example

Advertisements