Friday, March 10, 2006

Using ltrim is not the right answer sometimes.

In a PHP application I was debugging today for a company I found them using ltrim to remove a substring from the start of a string which was producing weird results such as "tal" after invoking with "Total".

When I asked their programmer why is he using ltrim, he replied to strip out a substring from the start of the string.

His code was:

$clean=ltrim($unclean, "To");



So what's the problem? If a string starts with To such as in "ToTotal" (hypothetically), it incorrectly returns "tal" when the needed result was "Total" with just the first part of To stripped.

I've posted the correct way to strip a substring using regular expressions.
  post to Del.icio.us

45 Comments:

At 1:08 AM, Blogger 快車 said...

幸福不是一切,人還有責任。 ..................................................

 
At 5:22 AM, Blogger 糟糕啦 said...

一隻穿雲箭 千軍萬馬來相見 這麼棒的文章 幫你頂先!..............................

 
At 8:48 PM, Blogger chat said...

來逛逛blog~~跟您打聲招呼........................................

 
At 2:27 AM, Blogger 玄雨 said...

I love readding, and thanks for your artical...................................................

 
At 8:05 AM, Blogger 憲妤憲妤 said...

以簡單的行為愉悅他人的心靈,勝過千人低頭禱告........................................

 
At 8:19 AM, Blogger 婉菁婉菁婉菁 said...

I do like ur article~!!!...................................................

 
At 12:08 AM, Blogger 睿玄 said...

分享笑話三則~
唐僧徵婚
原來還是個母鷹
俺的愛人不是豬

 
At 12:25 PM, Blogger 逸凡逸凡 said...

Actions speak louder than words. ........................................

 
At 7:21 PM, Blogger 和中如勇 said...

sex女,ut,貼影,av,高潮,女優,做愛,手機成人影片,色遊戲,成人動漫,百分百貼圖區,85cc影片,成人影音,av色情影片,A片線上,a片,sex,777,三級線上看,美女的照片,視訊做愛,78論壇,打飛機,免費視訊,成人影院,辣妹視訊,視訊聊天,影片網,kiss911,a片,交友,聊天,做愛,免費影片,性交,線上成人,網路色情,聊天,美女自慰,免費a分享,免費短片,裸照,一夜情,女優,85cc成人片,美女寫真,偷拍a,情人視訊聊天室

 
At 4:37 PM, Blogger 辰原 said...

你的部落格很棒,我期待更新喔........................................

 
At 4:54 PM, Blogger 瑤德 said...

書山有路勤為徑,學海無涯苦作舟..................................................

 
At 11:09 PM, Blogger BeauMiddleton11淑君 said...

Beauty, unaccompanied by virtue, is as a flower without perfume..............................................

 
At 6:23 AM, Blogger 天光天光 said...

珍惜當下..活在當下..祝大家都平平安安健健康康!........................................

 
At 1:18 AM, Blogger 熙辰 said...

好喜歡你的部落格唷,剛下班,要去睡了!!!掰~~..................................................

 
At 6:01 AM, Blogger ElisaCe慈佐lestin0708 said...

傻氣的人喜歡給心 雖然每次都被笑了卻得到了別人的心..................................................

 
At 10:08 PM, Blogger 瓊文TamMcfee0520 said...

所有的資產,在不被諒解時,都成了負債..................................................

 
At 3:28 PM, Blogger 瓊文瓊文 said...

人類最大的悲劇不是死亡,而是沒有掌握有意義的人生......................................................................

 
At 3:46 PM, Blogger 禎峰 said...

這個時代,不缺乏感傷,但缺乏反思~~希望能多看到值得思考.................................................................

 
At 5:38 AM, Blogger 江婷 said...

所有的資產,在不被諒解時,都成了負債.................................................................                           

 
At 1:10 PM, Blogger 江婷 said...

與人相處不妨多用眼睛說話,多用嘴巴思考.................................................................

 
At 4:42 AM, Blogger 郁雯郁雯 said...

成熟,就是有能力適應生活中的模糊。.................................................................

 
At 7:44 AM, Blogger 建霖建霖 said...

走過路過~不能錯過~哈哈............................................................

 
At 8:09 PM, Blogger 云依恩HFH謝鄭JTR安 said...

向著星球長驅直進的人,反比踟躕在峽路上的人,更容易達到目的。............................................................

 
At 9:07 PM, Blogger 健豪健豪 said...

憤怒,是片刻的瘋狂。..................................................

 
At 12:12 AM, Blogger 香昱信張君林 said...

感謝不吝分享您的心得............................................................

 
At 12:14 AM, Blogger 趙坤然趙坤然 said...

單純喜歡你的部落格 留個言跟您問聲好~~............................................................

 
At 2:59 PM, Blogger richardsbishop20199 said...

要用心經營哦~~祝福你~~
............................................................

 
At 11:51 PM, Blogger 姿柯瑩柯dgdd憶曾g智曾 said...

朋友是一面鏡子............................................................

 
At 3:14 PM, Blogger 銘木 said...

連接生與死這兩塊陸地的橋樑是愛…….......................................................

 
At 3:03 AM, Blogger ju吳phe宇te佳ns said...

安安唷~~幸運的日子送給妳(你)滿滿的幸福,也祝福你天天都開心唷............................................................

 
At 12:07 AM, Blogger 承蘋承蘋 said...

不要去想沒拿到的東西,多想想自己手裡所擁有的..................................................

 
At 12:06 AM, Blogger 黃信梅 said...

良言一句三冬暖,惡語傷人六月寒。......................................................................

 
At 12:06 AM, Blogger 鄭信弘 said...

愛,拆開來是心和受兩個字。用心去接受對方的一切,用心去愛對方的所有。......................................................................

 
At 12:07 AM, Blogger 徐禎緯 said...

拒絕冒險和成長的人,終將被生命的潮流陶汰。..................................................

 
At 4:41 AM, Blogger 凱v胡倫 said...

在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

 
At 2:14 AM, Blogger 文王廷 said...

缺少智慧,就是缺少一切..................................................

 
At 9:51 PM, Blogger 佳陳容 said...

Circumstances are the rulers of the weak, instrument of the wise.............................................................

 
At 2:34 PM, Blogger 黃智樺黃智樺 said...

財富並非永遠的朋友,但朋友卻是永遠的財富。......................................................................

 
At 11:14 PM, Blogger 惠桂刁惠桂刁惠桂刁 said...

雖天地之大,萬物之多,而惟吾蜩翼之知。..................................................

 
At 11:23 PM, Blogger 怡靜怡靜怡靜怡雯 said...

讓好心情回味發酵;壞心情留在文字裡隨時間消逝吧!............................................................

 
At 3:36 AM, Blogger 璇陳陳陳竹 said...

More haste, less speed..................................................................

 
At 1:35 PM, Blogger 于庭吳 said...

嘿,你的部落格不錯哦!............................................................

 
At 12:59 AM, Blogger 黃英吳思潔吳思潔邦 said...

人生中最好的禮物就是屬於自己的一部份............................. ....................................

 
At 7:07 AM, Blogger Best Business Brands said...

What is wrong with this statement that it is still giving me spaces after ... Judging from the code, it seems that you may be trying to strip spaces and ...

 
At 6:27 AM, Blogger Sji Infotech Pvt Ltd. said...

This comment has been removed by the author.

 

Post a Comment

<< Home

eXTReMe Tracker