Welcome to Server Circle. It's a friendly site and all levels of experience are welcome. Be aware that we use cookies for your login.
Server Circle - Ask questions about Servers and get answers from experts.
Beta (0.08 sec)
Splitting a sentence in an array up by words in PHP

Can I split this up:

"I am a PHP newbie"

so each word gets put into a variable of its own ?

Thanks.
Asked by:
JackFlash
378 points
 Report Abuse
 Share Page - Category: Programming - Tags: Splitting a sentence in an array up by words in PHP
 Enter your response
Please use Pastie.org to paste lengthy code or to fix formatting issues with code
  • Responses in reverse (2)

Personally I would just use something like explode(), eg:

$sentence = "I am a PHP newbie";

$split_sentence = explode(' ', $sentence);

You could then reference the words thus:

$split_sentence[0] -> I

$split_sentence[1] -> am

$split_sentence[2] -> a

$split_sentence[3] -> PHP

$split_sentence[4] -> newbie

Or add print_r($split_sentence) to see the output.

Each() works if you've already gotten the words into an array such as that above, but you need to get them there first.

Response by:
trevHCS
640 points
Ahh ....



$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");

$bar = each($foo);

print_r($bar);

Response by:
JackFlash
378 points


  • Related Questions
About Us : Contact Us : Etiquette : Terms : CDN Failover : ShorterURL : CDN Fallback : © 2013 Server Circle