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

How to remove spaces from string in php

王林
王林Original
2020-08-15 15:07:192979browse

php method to remove spaces in a string: You can use the str_replace() function to achieve this. The str_replace() function is used to replace some characters in a string (case-sensitive) and returns a string or array with the replacement value.

How to remove spaces from string in php

str_replace() function replaces some characters in a string (case-sensitive) and returns a string or array with the replacement value.

(Recommended tutorial: php graphic tutorial)

Syntax:

str_replace(find,replace,string,count)

Parameters:

  • find Required. Specifies the value to look for.

  • replace Required. Specifies the value to replace the value in find .

  • #string Required. Specifies the string to be searched for.

  • #count Optional. A variable counting the number of substitutions.

(Video tutorial recommendation: php video tutorial)

Implementation code:

header("content-type:text/html;charset=utf-8");
$str = "      PHP     中     文   网    ";
echo $str . "<br />";
$re=str_replace(" ",&#39;&#39;,$str);
echo $re
?>

Running result:

How to remove spaces from string in php

The above is the detailed content of How to remove spaces from 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