教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php中删除字符首尾空格的方法

php中删除字符首尾空格的方法

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供php中删除字符首尾空格的方法等资源,欢迎您收藏本站,我们将为您提供最新的php中删除字符首尾空格的方法资源
在php中要删除字符空间有很多的方法可用,我来介绍利用mb_ereg_replace()和ltrim,rtrim,trim这三个函数的处理方法。

实例

<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('copy1594')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1594>

<?
$str="     网页制作教程 www.jiaochengji.com     ";
$str = mb_ereg_replace('^( | ) ', '', $str);
$str = mb_ereg_replace('( | ) $', '', $str);
echo mb_ereg_replace('  ', "n  ", $str);
?>

有些朋友可能对mb_ereg_replace()没看明白,下面我们来介绍一下mb_ereg_replace这个函数吧

mb_ereg_replace()我们只要注意前面的mb就好了,有些朋友用过字符转换的一看就明白了这个函数是支持中文。

有些朋友会问php不是有自己的函数么,下面我们看实例

实例

<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('copy5790')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5790>

<?php
$str=" 去除前后空格 ";
echo "方括号中为原始字符串:[".$str."]<br>";
echo "原始字符串长度:".strlen($str)."<br>";
$str1=ltrim($str);
echo "执行ltrim()之后的长度:".strlen($str1)."<br>";
$str2=rtrim($str);
echo "执行rtrim()之后的长度:".strlen($str2)."<br>";
$str3=trim($str);
echo "执行trim()之后的长度:".strlen($str3)."<br>";
echo "去掉首尾空格之后的字符串:[".$str3."]";
?>

总结:同一个问题会有多种解决方法,就像我们删除字符空格一样,可以用两种不同的方法达到想同的效果了。

您可能感兴趣的文章:
python怎样去除空格和换行符
js 禁止文本框输入空格的代码
js去掉空格的代码
python中strip()函数有什么用法
php去掉字符串首尾空格的方法
php删除空格空行的例子
PHP中清除字符串所有空格程序代码
你真的了解strip()、lstrip()、rstrip()函数吗?
js去掉字符串的空格或换行符(附相关正则介绍)
php中删除字符首尾空格的方法

[关闭]
~ ~