There are many ways to delete character spaces in PHP. Let me introduce the processing method using the three functions of mb_ereg_replace() and ltrim, rtrim, and trim.
Example
The code is as follows
代码如下 |
复制代码 |
$str=" 网页制作教程 www.bKjia.c0m ";
$str = mb_ereg_replace('^( | )+', '', $str);
$str = mb_ereg_replace('( | )+$', '', $str);
echo mb_ereg_replace(' ', "n ", $str);
?>
|
|
Copy code
|
$str=" Web page production tutorial www.bKjia.c0m ";
$str = mb_ereg_replace('^(| )+', '', $str);
$str = mb_ereg_replace('( | )+$', '', $str);
echo mb_ereg_replace(' ', "n ", $str);
?>
Some friends may not understand mb_ereg_replace(). Let’s introduce the mb_ereg_replace function
代码如下 |
复制代码 |
$str=" 去除前后空格 ";
echo "方括号中为原始字符串:[".$str."] ";
echo "原始字符串长度:".strlen($str)." ";
$str1=ltrim($str);
echo "执行ltrim()之后的长度:".strlen($str1)." ";
$str2=rtrim($str);
echo "执行rtrim()之后的长度:".strlen($str2)." ";
$str3=trim($str);
echo "执行trim()之后的长度:".strlen($str3)." ";
echo "去掉首尾空格之后的字符串:[".$str3."]";
?>
|
mb_ereg_replace() We only need to pay attention to the mb in front. Some friends who have used character conversion will understand at a glance that this function supports Chinese.
| Some friends will ask if PHP has its own functions. Let’s look at examples below
Example
The code is as follows
|
Copy code
$str="Remove leading and trailing spaces";
echo "The original string in square brackets: [".$str."] ";
echo "Original string length:".strlen($str)." ";
$str1=ltrim($str);
echo "The length after executing ltrim():".strlen($str1)." ";
$str2=rtrim($str);
echo "The length after executing rtrim():".strlen($str2)." ";
$str3=trim($str);
echo "The length after executing trim():".strlen($str3)." ";
echo "The string after removing the leading and trailing spaces: [".$str3."]";
?>
Summary: There are multiple solutions to the same problem. Just like when we delete character spaces, we can use two different methods to achieve the same effect.
http://www.bkjia.com/PHPjc/629135.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629135.htmlTechArticleThere are many ways to delete character spaces in php. Let me introduce the use of mb_ereg_replace() and ltrim, rtrim , trim processing methods of these three functions. Example code is as follows Copy code ? $...
|
|