Jump to content

Archived

This topic is now archived and is closed to further replies.

flower.jason

Splitting text into words

Recommended Posts

Hi,

Just wondering if someone knows a function which would split my text into words so i could do this


Text...          Hello my name is jason

To                Hello.my.name.is.[color=blue]jason[/color]

Share this post


Link to post
Share on other sites

[code]
<?php

$string = 'Hello my name is jason';
$str_array = explode(' ',$string);
$format = implode(".", $str_array);

echo"$format";

?>[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Building off of redarrow, you could also do:

[code]$text = "Hello my name is jason";
echo str_replace(" ", ".", $text);[/code]

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.