phpSensei Posted January 4, 2008 Share Posted January 4, 2008 Whats the best way of taking a sentence, breaking all the words into alphabets, and still keeping the spaces... Example My name is Sensei Result M|y| n|a|m|e i|s| S|e|n|s|e|i Quote Link to comment https://forums.phpfreaks.com/topic/84540-solved-best-way-of-doing-this/ Share on other sites More sharing options...
phpSensei Posted January 4, 2008 Author Share Posted January 4, 2008 http://www.w3schools.com/php/func_string_strtok.asp Splits the words, now i need to split it into smaller pieces.. Quote Link to comment https://forums.phpfreaks.com/topic/84540-solved-best-way-of-doing-this/#findComment-430719 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2008 Share Posted January 4, 2008 Here's one way, I don't know if it's the best... <?php $str = 'My name is Sensei'; $tmp = array(); for ($i=0;$i<strlen($str);$i++) $tmp[] = $str[$i]; echo str_replace(' |',' ',implode('|',$tmp)); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/84540-solved-best-way-of-doing-this/#findComment-430723 Share on other sites More sharing options...
phpSensei Posted January 4, 2008 Author Share Posted January 4, 2008 Props to you my brother. Quote Link to comment https://forums.phpfreaks.com/topic/84540-solved-best-way-of-doing-this/#findComment-430731 Share on other sites More sharing options...
Psycho Posted January 4, 2008 Share Posted January 4, 2008 kenrbnsn already beat me to it, but I was taking a different approach and was having problem handling the spaces until I saw kenrbnsn elegant solution. here is a one line solution <?php $input = "My name is Sensei"; echo str_replace(' |', ' ', implode('|', str_split($input))); ?> Quote Link to comment https://forums.phpfreaks.com/topic/84540-solved-best-way-of-doing-this/#findComment-430755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.