Home > Backend Development > PHP Problem > How to remove spaces in string in php

How to remove spaces in string in php

青灯夜游
Release: 2023-03-08 22:38:01
Original
2268 people have browsed it

php method to delete spaces in a string: 1. Use str_ireplace() function, syntax "str_ireplace(' ', '', $str)"; 2. Use str_replace() function, syntax "str_replace( ' ', '', $str)".

How to remove spaces in string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Method 1: Use str_ireplace() Function

<?php
$str = &#39;hello world !&#39;;
$search = &#39; &#39;;
$replace = &#39;&#39;;
echo str_ireplace($search, $replace, $str);
?>
Copy after login

Output:

helloworld!
Copy after login
Copy after login

Method 2: Use str_replace() function

<?php
$str = &#39;hello world !&#39;;
$str = str_replace(&#39; &#39;, &#39;&#39;, $str);
echo $str;
?>
Copy after login

Output:

helloworld!
Copy after login
Copy after login

Recommended learning: "PHP Video Tutorial"

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template