![]() |
Truncate String - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Truncate String (/showthread.php?tid=9467) |
Truncate String - El Forum - 06-26-2008 [eluser]Yash[/eluser] In searching I need to truncate rest of the string after I found a predefined string. I mean "Hey coding is fun and I love it." Now I need to break string after "and" so I want only "hey coding is fun" My code is Code: $content="Hey coding is fun and I love it."; Any more idea I mean If I don't want to break string or don't want array.. Truncate String - El Forum - 06-26-2008 [eluser]xwero[/eluser] Code: $strarray = str_word_count($str, 2); And the rest you can figure out yourself ![]() edit : you can't have false as the third parameter of substr ![]() Truncate String - El Forum - 06-26-2008 [eluser]Yash[/eluser] My New Code : Code: $findme = '<|more|>'; Truncate String - El Forum - 06-26-2008 [eluser]xwero[/eluser] You can compact the code Code: $findme = '<|more|>'; Truncate String - El Forum - 06-26-2008 [eluser]Yash[/eluser] Bingo..I like that Thank you Truncate String - El Forum - 06-26-2008 [eluser]Pascal Kriete[/eluser] I know this is solved, but that just makes me cringe. You can explode at full strings, not just characters: Code: $findme = '<|more|>'; Truncate String - El Forum - 06-26-2008 [eluser]xwero[/eluser] nice! Truncate String - El Forum - 06-26-2008 [eluser]Yash[/eluser] Which is FASTER ...I'll go for that. nice coding skill.. Thank you Truncate String - El Forum - 06-26-2008 [eluser]Yash[/eluser] [quote author="inparo" date="1214497942"]I know this is solved, but that just makes me cringe. You can explode at full strings, not just characters: Code: $findme = '<|more|>'; This one is really fast. I performed benchmark testing.result this code is winner compare to my previous one. Thank you Truncate String - El Forum - 06-26-2008 [eluser]sophistry[/eluser] sorry! i couldn't resist. i benchmarked this at around 20% faster with small strings; there's no need to go into array land at all. this will stay fast even if the input string $content is huge while the array functions will begin to bog down (even with the limit param of explode()). Code: $findme = '<|more|>'; EDIT: btw, make sure to use xwero's example of this string-centric technique above because it has some error trapping. that may be why the benchmarks put it slower than inparo's explode() approach. EDIT: actually i just went back and tested and you don't need error trapping at all in either approach - both nested functions handle varied text input conditions without crying. |