Home  >  Article  >  Backend Development  >  How to filter all whitespace characters in php

How to filter all whitespace characters in php

墨辰丷
墨辰丷Original
2018-06-06 15:58:531981browse

This article mainly introduces the method of filtering all whitespace characters in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The trim function that comes with PHP can only replace the spaces at the left and right ends. I feel that it is not very easy to use in some cases. If you want to filter out all the whitespace characters in a string (spaces, full-width spaces, line break, 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.

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

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Definition and usage of the htmlentities() function in php

How to generate a verification code that can be clicked to refresh in PHP And a simple example

Detailed explanation of how php generates a QR code that can be logged in by scanning with the Android client

The above is the detailed content of How to filter all whitespace characters 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