Home > Backend Development > PHP Tutorial > How to Strip Whitespace Characters from a String in PHP?

How to Strip Whitespace Characters from a String in PHP?

Linda Hamilton
Release: 2024-11-12 15:06:02
Original
511 people have browsed it

How to Strip Whitespace Characters from a String in PHP?

Stripping Whitespace Characters from a String

How can you strip all whitespace characters from a string in PHP?

Problem

The php_strip_whitespace() function and the preg_replace() function with a whitespace character are not working.

Answer

To strip any whitespace character, use a regular expression:

$str = preg_replace('/\s+/', '', $str);
Copy after login

Note that this regular expression also handles whitespace in UTF-8 strings.

The above is the detailed content of How to Strip Whitespace Characters from a String in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template