Home > php教程 > PHP源码 > body text

php去除字符串首尾中英文空格程序

WBOY
Release: 2016-06-08 17:23:06
Original
1220 people have browsed it

下面本文章来给各位同学总结了几种php去除字符串首尾中英文空格程序实例,这里有用正则替换与trim系列函数删除,下面我们来看看。

<script>ec(2);</script>

例1.trim函数删除空格


trim()函数用于去除字符串开始位置以及结束位置的空格,并返回去掉空格后的字符串。语法如下:
string trim(string str[,string charlist]);
ltrim()函数用于去除字符串左边的空格或指定字符串。语法如下:
string ltrim(string str[,string charlist]);
rtrim()函数用于去除字符串右边的空格和特殊字符。语法如下:
string rtrim(string str[,string charlist]);

 代码如下 复制代码
 
$a="(a,b,c,)";
echo $a."
"; //输出:(a,b,c,)
$b=trim($a,"()"); //去除字符串首尾含有的字符“(”或“)”
echo $b."
"; //输出:a,b,c,
$c=trim($a,"(,)"); //去除字符串首尾含有的字符“(”、“,”或“)”
echo $c."
"; //输出:a,b,c
 
?>

 
输出结果:
(a,b,c,)
a,b,c,
a,b,c
在sql中函数trim()用途为去除首尾空格,ltrim()为去除字符串左侧空格,rtrim()为去除字符串右侧空格。

 

例2.利用str_replace正则替换

 代码如下 复制代码

function  mbTrim($str) 

        return mb_ereg_replace('(^( | )+|( | )+$)', '', $str); 

例3.str_replace正则替换

 代码如下 复制代码


$str="      www.111cn.net     ";
$str = mb_ereg_replace('^( | )+', '', $str);
$str = mb_ereg_replace('( | )+$', '', $str);
echo mb_ereg_replace('  ', "n  ", $str);
?>

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!