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] Quote Link to comment 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] Quote Link to comment 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] Quote Link to comment 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] Quote Link to comment 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] Quote Link to comment 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? Quote Link to comment 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] Quote Link to comment 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] Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
flower.jason Posted August 2, 2006 Author Share Posted August 2, 2006 thankyou everyone. That is fantastic!! Quote Link to comment 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.