flower.jason Posted August 2, 2006 Share Posted August 2, 2006 Hi,Just wondering if someone knows a function which would split my text into words so i could do thisText... Hello my name is jasonTo Hello.my.name.is.[color=blue]jason[/color] Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/ Share on other sites More sharing options...
Caesar Posted August 2, 2006 Share Posted August 2, 2006 [code]<?php$string = 'Hello my name is jason';$str_array = explode(' ',$string);$format = implode(".", $str_array);echo"$format";?>[/code] Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67516 Share on other sites More sharing options...
hitman6003 Posted August 2, 2006 Share Posted August 2, 2006 use explode to seperate each word, then implode to join them by a new seperator:[code]$text = "Hello my name is jason";$t = explode(" ", $text);echo implode(".", $t);[/code] Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67517 Share on other sites More sharing options...
redarrow Posted August 2, 2006 Share Posted August 2, 2006 were ever it says my name is the reqired . will be ok.[code]$replace_words=str_replace("Hello my name is ","Hello.my.name.is.",$text);[/code] Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67518 Share on other sites More sharing options...
hitman6003 Posted August 2, 2006 Share Posted August 2, 2006 Building off of redarrow, you could also do:[code]$text = "Hello my name is jason";echo str_replace(" ", ".", $text);[/code] Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67520 Share on other sites More sharing options...
flower.jason Posted August 2, 2006 Author Share Posted August 2, 2006 Thankyou!!!!Just one thing.. not a big issue.Could i make it so the last word of the sentance in say blue? Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67525 Share on other sites More sharing options...
hitman6003 Posted August 2, 2006 Share Posted August 2, 2006 [code]$text = "Hello my name is jason";$t = explode(" ", $text);$n = count($t);$t[$n-1] = '<span style="color: blue;">' . $t[$n-1] . '</span>';echo implode(".", $t);[/code] Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67527 Share on other sites More sharing options...
corbin Posted August 2, 2006 Share Posted August 2, 2006 Aww someone posted while i was typing this... anyways this will do the samething as hitman's thing...[code]<?$arr = ("This is a sentence");$arr = explode(" ", $arr);$c = count($arr);$arr[$c - 1] = "<font color=\"blue\">" . $arr[$c - 1] . "</font>";$arr = implode(".", $arr);echo $arr;?>[/code]would return [code]This.is.a.<font color="blue">sentence</font>[/code] Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67531 Share on other sites More sharing options...
nethnet Posted August 2, 2006 Share Posted August 2, 2006 Probably a better way, but this works:[quote]<?php$string = "Whatever text you want here";$explode = explode(' ', $string);$count = count($explode) - 1;for($i=0; i<$count; $i++) echo "{$explode[$i]}.";echo "<font color=\"blue\">{$explode[$count]}</font>";?>[/quote]Of course, never use the <font> tag :-P Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67533 Share on other sites More sharing options...
flower.jason Posted August 2, 2006 Author Share Posted August 2, 2006 thankyou everyone. That is fantastic!! Link to comment https://forums.phpfreaks.com/topic/16280-splitting-text-into-words/#findComment-67535 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.