Home  >  Article  >  Backend Development  >  How to remove spaces in string in php

How to remove spaces in string in php

青灯夜游
青灯夜游Original
2021-03-24 18:31:582223browse

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);
?>

Output:

helloworld!

Method 2: Use str_replace() function

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

Output:

helloworld!

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!

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