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 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.. 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 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. 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))); ?> 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
Archived
This topic is now archived and is closed to further replies.