Well yes and no. You can use it the way you like to split string.
<?php
$str = 'Hi there!|How are you?|Whats up?';
$pieces = explode('|',$str);
print_r($pieces);
?>
Will output:
Array
(
[0] => Hi there!
[1] => How are you?
[2] => Whats up?
)
So those are sentences. And the separator can be anything.
$str = "Hi there!separatorHow are you?separatorWhats up?";
$pieces = explode('separator',$str);
print_r($pieces);
That will give you the same output as before.
So when you split with space, it will give you all the words AND characters in array.
Example:
$str = 'What the #ยค%& is going on?';
$pieces = explode(' ',$str);
print_r($pieces);
Output:
Array
(
[0] => What
[1] => the
[2] => #ยค%&
[3] => is
[4] => going
[5] => on?
)
So in that theres more than just words like for example number 5: "on?" That not really a word is it because it has questionmark at the end? Or am I nitpicking? ๐ Anyway, if you really want just the words, explode will give you a start and you can process those strings to strip the unwanted characters away.
Exlode is useful for many things. LikeTo separate words as you said. Today I made a script that takes form submission and separates different customer details from copy-pasted text from openoffice calc. Its also used to make "mini-database" files.
For example a gallery which has commenting. Each picture has a textfile containing handle, comment, and date added. And each comment are on their own line. For example:
cahva|Nice picture. Wish I were there|2006-07-09 20:55:00
somedude|Yeah, very nice pic!|2006-07-09 21:05:00
That you could parse first with file() function(that will give you file as array separated by lines) and further in the loop explode those something like this:
$commentfile = file('picture32.txt');
foreach($commentfile as $comment) {
list($nick,$comment,$date) = explode('|',$comment);
echo '<h3>'.$nick.' on '.$date.'</h3>';
echo '<p>'.$comment.'</p>';
}
Huh, long post yet so simple issue ๐