Re: Divide into words
| From: | Marco Tabini | Date: | Mon, 16 Dec 2002 17:19:06 +0000 |
| Subject: | Re: Divide into words | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-128432@lists.php.net to get a copy of this message | ||
You could use strpos--there are easier ways if you don't need the
offsets.
Example:
<?php
$inputstring = " This is a string ";
$pos = 0;
$oldpos = 0;
$result = array();
$inputstring .= ' ';
while (!(($pos = (strpos ($inputstring, ' ', $pos))) === false))
{
if ($pos != $oldpos)
$result [substr ($inputstring, $oldpos, $pos - $oldpos)] = $oldpos;
$pos++;
$oldpos = $pos;
}
print_r ($result);
?>
This is from memory, but it looks like it should work even for weird
strings like the one above.
Cheers,
Marco
--
------------
php|architect - The magazine for PHP Professionals
The monthly worldwide magazine dedicated to PHP programmers
Come visit us at http://www.phparch.com!