Jump to content


Photo

Splitting text into words


  • Please log in to reply
9 replies to this topic

#1 flower.jason

flower.jason
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 August 2006 - 01:51 AM

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.jason

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 02 August 2006 - 01:55 AM


<?php

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

echo"$format";

?>

PHP Ninja

#3 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 02 August 2006 - 01:56 AM

use explode to seperate each word, then implode to join them by a new seperator:

$text = "Hello my name is jason";
$t = explode(" ", $text);
echo implode(".", $t);


#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 02 August 2006 - 01:56 AM

were ever it says my name is the reqired . will be ok.


$replace_words=str_replace("Hello my name is ","Hello.my.name.is.",$text);

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 02 August 2006 - 01:58 AM

Building off of redarrow, you could also do:

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


#6 flower.jason

flower.jason
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 August 2006 - 02:06 AM

Thankyou!!!!

Just one thing.. not a big issue.


Could i make it so the last word of the sentance in say blue?

#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 02 August 2006 - 02:10 AM

$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);


#8 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 02 August 2006 - 02:18 AM

Aww someone posted while i was typing this... anyways this will do the samething as hitman's thing...

<?
$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;
?>

would return
This.is.a.<font color="blue">sentence</font>

Why doesn't anyone ever say hi, hey, or whad up world?

#9 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 02 August 2006 - 02:19 AM

Probably a better way, but this works:

<?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>";

?>


Of course, never use the <font> tag :-P
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#10 flower.jason

flower.jason
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 August 2006 - 02:20 AM

thankyou everyone. That is fantastic!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users