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:
Post Tags: how, to, break, down, a, sentence, into, individual, words, with, php, string
This Post Has Been Viewed 159 Times Since Wed Jun 20, 2007 7:00 am Posted By hostman with 0 replies
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

Leave Your Comments     [ dejar commentarios ]
  * Name     [nombre]

  * eMail (will not be published)     [coreo electronico]

* Enter Your Reply or Comments:    [commentarios]


Add Picture To Comments         [incluir foto]
YES NO             upload
Receive Replies on my Comments (An email will be sent to you when someone replies to your comments)

     

Comments and replies About How To Break Down A Sentence Into Individual Words With Php String




(0) Comments