Home >Backend Development >PHP Problem >How to replace spaces in php

How to replace spaces in php

王林
王林Original
2020-11-03 11:13:244292browse

How to replace spaces in php: You can use the preg_replace() function combined with the regular expression [/\s /] to replace spaces, such as [preg_replace('/\s /',' ',$contents) 】.

How to replace spaces in php

Function introduction:

preg_replace function performs a regular expression search and replacement.

(Recommended tutorial: php video tutorial)

Example:

Replace all spaces with empty spaces

$contents="    abc           ";
$contents=preg_replace('/\s+/','',$contents);
//结果$contents="abc";

Related Recommended: php training

The above is the detailed content of How to replace spaces in php. For more information, please follow other related articles on the PHP Chinese website!

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