php 去除空余字符,PHP 删除字符串末端的空白字符(或者其他字符)

用户评论:

pinkgothic at gmail dot com (2013-07-01 09:10:58)

On the recurring subject of string-stripping instead of character-stripping rtrim() implementations... the simplest (with a caveat) is probably the basename() function. It has a second parameter that functions as a right-trim using whole strings:

...outputs 'Moo'.

Since it also strips anything that looks like a directory, it's not quite identical with hacking a string off the end:

...still outputs 'Moo'.

But sometimes it gets the job done.

pLIMP (2012-07-20 19:49:34)

Function similar to rtrim only this will truncate the string at the 1st occurence of any character from $charlist

foreach ($charlistas$char) {$pos=min(strpos($string,$char),$pos);

}$string_stripped=substr($string,0,$pos);

return$string_stripped;

}?>

krzysiek dot 333 at gmail dot com (2012-05-01 09:58:42)

function read_more($in,$len=160){

if(strlen($in)>$len){

return preg_replace('/[\s\.,][^\s\.,]*$/u', '', substr($in, 0, $len)).'...';

}else{

return $in;

}

}

echo read_mode("Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi. Cras vel lorem. Etiam pellentesque aliquet tellus. Phasellus pharetra nulla ac diam. Quisque semper justo at risus. Donec venenatis, turpis vel hendrerit interdum, dui ligula ultricies purus, sed posuere libero dui id orci.");

/* Output:

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Proin nibh augue, suscipit a, scelerisque sed, lacinia in, mi. Cras vel lorem. Etiam pellentesque...*/

NBS Studio (2010-02-22 05:02:59)

The simplest way to strip a newline form a text file is ltrim();

trim or explode or split ("\n" or "\r\n") doesn't work in all cases, so give ltrim(); a try instead.

info at nbs-studio dot com (2010-02-22 05:02:41)

The simplest way to strip a newline form a text file is ltrim();

trim or explode or split ("\n" or "\r\n") doesn't work in all cases, so give ltrim(); a try instead.

pinkgothic at gmail dot com (2010-01-22 05:36:42)

I have an obsessive love for php's array functions given how extremely easy they've made complex string handling for me in various situations... so, have another string-rtrim() variant:

} while (empty($last) && (count($lines)));// re-assemble what remainsreturnimplode($strip,array_merge($lines, array($last)));

}?>

Astonishingly, something I didn't expect, but: It completely compares to harmor's rstrtrim below, execution time wise. o_o Whee!

harmor (2008-04-04 15:05:07)

I'm sure there's a better way to strip strings from the end of strings.

* Strip a string from the end of a string

*

* @param string $str      the input string

* @param string $remove   OPTIONAL string to remove

*

* @return string the modified string

*/functionrstrtrim($str,$remove=null)

{$str= (string)$str;$remove= (string)$remove;

if(empty($remove))

{

returnrtrim($str);

}$len=strlen($remove);$offset=strlen($str)-$len;

while($offset>0&&$offset==strpos($str,$remove,$offset))

{$str=substr($str,0,$offset);$offset=strlen($str)-$len;

}

returnrtrim($str);

}//End of function rstrtrim($str, $remove=null)echorstrtrim('Hello World!!!','!')   .'
';//"Hello World"echorstrtrim('Hello World!!!','!!')  .'
';//"Hello World!"echorstrtrim('Hello World!!!','!!!') .'
';//"Hello World"echorstrtrim('Hello World!!!','!!!!').'
';//"Hello World!!!"?>

YAS (2006-05-08 06:01:55)

To remove an unwanted character - example "." - if exist or not.

The example above doesn't include the case where there is no "."

If there is not "." at the example above the last word will be deleted.

Have fun with this code.

$text="This string contains. some unwanted characters on the end .";$text=trim($text);$last=$text{strlen($text)-1};

if (!strcmp($last,"."))

{$text=rtrim($text,'a..z');$text=rtrim($text,'.');

}?>

gbelanger at exosecurity dot com (2006-02-17 14:31:57)

True, the Perl chomp() will only trim newline characters. There is, however, the Perl chop() function which is pretty much identical to the PHP rtrim()

---

Here's a quick way to recursively trim every element of an array, useful after the file() function :

foreach ($aFileContentas$sKey=>$sValue) {$aFileContent[$sKey] =rtrim($sValue);

}print_r($aFileContent);?>

Unimagined at UnaimaginedDesigns dot Com (2005-01-16 00:49:26)

I needed a way to trim all white space and then a few chosen strings from the end of a string.  So I wrote this class to reuse when stuff needs to be trimmed.

functioncleaner($cuts,$pinfo) {$ucut="0";$lcut="0";

while ($cuts[$ucut]) {$lcut++;$ucut++;

}$lcut=$lcut-1;$ucut="0";$rcut="0";$wiy="start";

while ($wiy) {

if ($so) {$ucut="0";$rcut="0";

unset($so);

}

if (!$cuts[$ucut]) {$so="restart";

} else {$pinfo=rtrim($pinfo);$bpinfol=strlen($pinfo);$tcut=$cuts[$ucut];$pinfo=rtrim($pinfo,"$tcut");$pinfol=strlen($pinfo);

if ($bpinfol==$pinfol) {$rcut++;

if ($rcut==$lcut) {

unset($wiy);

}$ucut++;

} else {$so="restart";

}

}

}$this->cleaner=$pinfo;

}

}$pinfo="Well... I'm really bored...
     \n\t 
     \r\r 
\r
\r      \n
\t";$cuts= array('\n','\r','\t',' ',' ',' ','
','
','
');$pinfo= newcleaner($cuts,$pinfo);$pinfo=$pinfo->cleaner;

print$pinfo;?>

That class will take any string that you put in the $cust array and remove it from the end of the $pinfo string.  It's useful for cleaning up comments, articles, or mail that users post to your site, making it so there's no extra blank space or blank lines.

todd at magnifisites dot com (2003-08-19 18:19:12)

This shows how rtrim works when using the optional charlist parameter:

rtrim reads a character, one at a time, from the optional charlist parameter and compares it to the end of the str string. If the characters match, it trims it off and starts over again, looking at the "new" last character in the str string and compares it to the first character in the charlist again. If the characters do not match, it moves to the next character in the charlist parameter comparing once again. It continues until the charlist parameter has been completely processed, one at a time, and the str string no longer contains any matches. The newly "rtrimmed" string is returned.

// If you were expecting the result to be 'This is a short ',

// then you're wrong; the exact string, 'short sentence',

// isn't matched.  Remember, character-by-character comparison!

// Example 2:rtrim('This is a short short sentence','cents');// returns 'This is a short short '?>

HW (2003-06-05 18:32:49)

$text="This string contains some unwanted characters on the end.";$text1=rtrim($text,'a..z');$text1=rtrim($text1,'.');

echo$text1;// only the '.' is trimmed.$text2=rtrim($text,'a..z.');

echo$text2;// The whole last word is trimmed.?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值