Home  >  Article  >  Backend Development  >  How to remove spaces and newlines in strings in php

How to remove spaces and newlines in strings in php

王林
王林Original
2020-08-27 14:47:534190browse

How to remove spaces and newlines in php strings: You can use the built-in function str_replace() to delete them. This function can replace some specific characters in the string with other characters and return a string with the replacement value, for example: [str_replace(PHP_EOL,'',$str)].

How to remove spaces and newlines in strings in php

The str_replace() function in php can replace some characters in a string with other characters (case-sensitive) and return a string with the replacement value or array.

(Recommended tutorial: php video 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

  • #count Optional. Variable that counts the number of substitutions

(Related recommendation:php training)

Example:

To remove newlines and spaces from a certain string.

$str = str_replace(PHP_EOL, '', $str);

Note: Line breaks in PHP can be replaced by PHP_EOL.

The above is the detailed content of How to remove spaces and newlines in strings 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