How To Break Down A Sentence Into Individual Words With Php String
how to break down a sentence into individual words with php string
how, to, break, down, a, sentence, into, individual, words, with, php, string
How To Break Down A Sentence Into Individual Words With Php String
Post Description: how to break down a sentence into individual words with php string
POST# 411
Posted On: Fri Feb 29, 2008 12:01 pm
web hosting
Topic: How To Break Down A Sentence Into Individual Words With Php String
today i will teach you how you can break down a whole sentence into separate words.

lets say i have a string called $string and this is how i declared it:

$string = "breaking down sentence into each word array";

well, if i wanted to break it down, i would have to manually do it like this:

$string1 = "breaking";
$string2 = "down ";
$string3 = "sentence ";
$string4 = "into ";
$string5 = "each ";
$string6 = "word ";
$string7 = "array";

oh, boy, there's gotta be an easier way to do this. i have good news for you, yes you can with a PHP function called explode();

here's and example script:
<?
$string = "breaking down sentence into each word array";
$words= explode(" ", $string );
echo $words[0]; // breaking
echo $words[1]; // down
?>


this would be the output:
OUTPUT:
breaking down


search:
Split a string by string
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
explode

(PHP 4, PHP 5)

explode � Split a string by string
Description
array explode ( string $delimiter, string $string [, int $limit] )

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
Parameters

delimiter

The boundary string.
string

The input string.
limit

If limit is set, the returned array will contain a maximum of limit elements with the last element containing the rest of string.

If the limit parameter is negative, all components except the last -limit are returned.

Although implode() can, for historical reasons, accept its parameters in either order, explode() cannot. You must ensure that the delimiter argument comes before the string argument.
Return Values

If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string, then explode() will return an array containing string.
ChangeLog

Version Description
5.1.0 Support for negative limits was added
4.0.1 The limit parameter was added

if you need more information, check out the the documentation at: http://www.php.net/manual/en/function.explode.php


No Repies
(0) Comments for How To Break Down A Sentence Into Individual Words With Php String

What do you think?

* name:  

* email:  

* Please enter comments:


Receive Replies on my Comments
(An email will be sent to you when someone replies to your comments)

Add image to comments
yes no             upload