教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php去除字符串首尾中英文空格程序

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

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供php去除字符串首尾中英文空格程序等资源,欢迎您收藏本站,我们将为您提供最新的php去除字符串首尾中英文空格程序资源
下面本文章来给各位同学总结了几种php去除字符串首尾中英文空格程序实例,这里有用正则替换与trim系列函数删除,下面我们来看看。

例1.trim函数删除空格


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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4652')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4652><?php
 
$a="(a,b,c,)";
echo $a."<br/>"; //输出:(a,b,c,)
$b=trim($a,"()"); //去除字符串首尾含有的字符“(”或“)”
echo $b."<br/>"; //输出:a,b,c,
$c=trim($a,"(,)"); //去除字符串首尾含有的字符“(”、“,”或“)”
echo $c."<br/>"; //输出:a,b,c
 
?>

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

 

例2.利用str_replace正则替换

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9219')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9219>

function  mbTrim($str) 

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

例3.str_replace正则替换

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1444')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1444>

<?
$str="      www.jiaochengji.com     ";
$str = mb_ereg_replace('^( | ) ', '', $str);
$str = mb_ereg_replace('( | ) $', '', $str);
echo mb_ereg_replace('  ', "n  ", $str);
?>

您可能感兴趣的文章:
php去除字符串首尾中英文空格程序
js 禁止文本框输入空格的代码
python怎样去除空格和换行符
PHP和sql去除字符串首尾特定字符或空格
php去掉字符串首尾空格的方法
PHP中字符串处理的一些常用函数
php字符串函数有哪些
PHP中清除字符串所有空格程序代码
php中如何去除字符串首尾字符?
PHP字符串函数与使用分析

[关闭]
~ ~