Home >Backend Development >PHP Tutorial >PHP filters all whitespace characters (spaces, full-width spaces, newlines, etc.), full-width newlines_PHP tutorial

PHP filters all whitespace characters (spaces, full-width spaces, newlines, etc.), full-width newlines_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:06:26907browse

php filters all whitespace characters (spaces, full-width spaces, newlines, etc.), full-width newlines

The trim function that comes with php can only replace the spaces at the left and right ends, it feels like In some cases, it doesn't work very well. If we want to filter out all whitespace characters in a string (spaces, double-width spaces, newlines, etc.), then we can write a filter function ourselves.

Everyone who has learned the str_replace function in PHP knows that it can be replaced in batches, so we can use the following source code to replace and filter all whitespace characters in a string.

<&#63;php
$str = 'jkgsd
gsgsdgs gsdg gsd';
echo myTrim($str);
function myTrim($str)
{
 $search = array(" "," ","\n","\r","\t");
 $replace = array("","","","","");
 return str_replace($search, $replace, $str);
}
&#63;>

Run the code and the page output: jkgsdgsgsdgsgsdggsd, which perfectly achieves the effect we want.

The above is how PHP filters all whitespace characters. I hope it will be helpful to everyone's learning.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1065579.htmlTechArticlephp filters all whitespace characters (spaces, full-width spaces, newlines, etc.), full-width newlines are included in php The trim function can only replace the spaces at the left and right ends, which seems to be inappropriate in some cases...
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