用户评论:

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.?>

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

  1. 删除字符串中出现次数最少的字符,汽水瓶,简单密码

    删除字符串中出现次数最少的字符 实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除.输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序. 输入描述:字符串只包含小写英文字母, ...

  2. 删除字符串中出现次数最少的字符

    在公交车上看一博客实现删除字符串中出现次数最少的字符,认为里面使用数组来作为hash很好,所以我就自己实现一遍. 要求:实现删除一个字符串中出现次数最少的字符.输出删除后的字符,要求安装原来顺序输出. ...

  3. C++实现删除字符串中所有重复出现的字符

    C++实现删除字符串中所有重复出现的字符 #include<iostream> using namespace std; char* DeleteRepeatCharacters(char ...

  4. JAVA----------------------华为机试--------------------------删除字符串中出现次数最少的字符...

    题目描述 实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除.输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序. 输入描述: 字符串只包含小写英文字母, 不考虑非法输入,输 ...

  5. HJ23 删除字符串中出现次数最少的字符

    描述 实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除.输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序. 注意每个输入文件有多组输入,即多个字符串用回车隔开 输入描述: ...

  6. C语言函数题- 删除字符串中下标为i的字符

    6-2 删除字符串中下标为i的字符 (10分) 本题要求实现一个函数,删除字符串中的下标为i的字符,i的值保证合法,即字符串一定存在下标为i的字符. 函数接口定义: void delstring ( ...

  7. 删除出现次数最少字符串JAVA_牛客网——华为机试(题23:删除字符串中出现次数最少的字符)(Java)...

    题目描述: 实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除.输出删除这些单词后的字符串,字符串中其它字符保持原来的顺序. 输入描述: 字符串只包含小写英文字母, 不考虑非法输入, ...

  8. 华为机试HJ23:删除字符串中出现次数最少的字符

    作者:翟天保Steven 版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处 题目描述: 实现删除字符串中出现次数最少的字符,若多个字符出现次数一样,则都删除.输出删除这些单 ...

  9. java 删除指定字符_字符串删除指定位置字符 JAVA 删除字符串中指定的字符

    <死侍2>有多不按套路出牌? 要CSS布局HTML小编今天和大家分享用到函数的调用. 编制函数fun,其功能是:删除一个字符串中指定的一.问题描述:从键盘输入一个字符串给str和一个字符给 ...

  10. 【C语言】字符串加密解密,字符串左旋,杨氏矩阵,删除字符串中出现次数最少的字符

    目录 1.字符串解密加密 2.字符串左旋 3.杨氏矩阵 4.删除字符串中出现次数最少的字符 好久不见,自从刷完剑指offer里面所有目前可以用C解决的题目吧之后,就在刷一些高校的题和华为机试题,一直在 ...

最新文章

  1. 398. Random Pick Index - LeetCode
  2. hibernate.hbm2ddl.auto配置详解
  3. 工程中新增Properties
  4. rtt 打印 float
  5. 转同事博客一则,随感
  6. Java操作mongoDB2.6的常见API使用方法
  7. hdu 1575 Tr A (二分矩阵)
  8. 天堂2单机版服务器维护,天堂2芙蕾雅服务端单机版(l2jAngel-CT2.6芙蕾雅-34)
  9. 【KnockoutJS】KnockoutJS 绑定列表数据。实现表头合并,列生成,图片上传等功能
  10. matlab画漫画,震惊!无聊男子竟用函数画出可爱的卡通猫
  11. 博客中常用的Emoji表情整理,欢迎自取
  12. php 操作 PSD,PHP中使用Imagick操作PSD文件实例
  13. 【使用 BERT 的问答系统】第 1 章 : 自然语言处理简介
  14. 标准盒模型 和怪异盒模型
  15. 树莓派4B从开箱到连接电脑(超级小白)
  16. windows画图板 ESL转RGB 实现区间渐变色
  17. yanzhenjie:permission:1.0.5 使用
  18. 键盘输入字符串 数字转换为 * 英文字母大写转小写 小写转大写
  19. js/Jquery通过MutationObserver实现监听dom元素的属性变化 用div简单实例
  20. 知识点滴 - 图形界面控件

热门文章

  1. 190115每日一句
  2. surf算子匹配物体
  3. Atitit 浏览器tech原理与概论 目录 1. 浏览器概述 1 1.1. 浏览器野史 UserAgent列传 1 1.2. 浏览器趋势 1 1.3. 浏览器大战 1 1.4. 三次浏览器大战 2
  4. Atitit 音频技术简史艾提拉著 目录 1. 2014年1月16日,谷歌发布音乐时间轴 2 2. 时代发展 2 2.1. 机械录音 电声录音时代 四.数码录音时代 2 3. 【音频录音技术】 2
  5. Atitit 数据库的历史与未来 目录 1.1. 两个对于数据库强需求的行业。电信 金融 1 1.2. 艾提拉分析 对数据库强需求行业金融 1 2. 数据库历史 2 2.1. ,上个世纪50,6
  6. Atitit aop spring5.2 demo与流程总结 目录 1.1. 定义切面MyAllAspect 1 1.2. 定义普通类型userservice 1 1.3. 设置切面到某个方法上ex
  7. Atitit 提升水平 把代码写的有技术含量 目录 1. 提高可读性(重要) 2 1.1. 异常模式代替返回值 2 1.2. Dsl 2 1.3. 流畅接口方法链 2 1.4. 层次结构抽象 2 1
  8. Atitit 软件国际化原理与概论
  9. 凯辉基金与法投行完成对资管软件公司NeoXam的投资 加速企业国际化发展布局
  10. 隐马尔科夫模型HMM学习最佳范例