Thanks! I was just reading that function in the manual when I got your reply. I tested it but I still have a couple of problems with it.
print_r(preg_split('(\s+)', " word1 word2 word3.", -1, PREG_SPLIT_DELIM_CAPTURE));
I get this:
Array ( [0] => [1] => word1 [2] => word2 [3] => word3. )
In [0] the spaces aren't there and it's splitting the other spaces too, where I need to only split the leading spaces. How should I write it?
TIA, Anguz
Kemper, Helmut wrote: > Hi, > > See preg_explite and use "/(\s+)/" for explode string into array. > > Tanks, > Kemper > > > ------------------------------------------------------------------------ > ----------------------------------------------------------------- > Helmut Kemper > [EMAIL PROTECTED] > Celular (Mobile): 55 81 99268744 > > > On 30/04/2004, at 20:22, Anguz wrote: > >> I have an array with many strings, of which most have spaces or tabs >> at the beginning, but no fixed number of them. Example: >> >> $arr[0] = ' Hello.'; >> >> How can I separate them into two strings? Like: >> >> Array >> ( >> [0] => Array >> ( >> [0] => " " //I added the quotes to notice the space. >> [1] => Hello. >> ) >> ) >> >> TIA! >> Anguz >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > >
...I wonder why you're doing this, but here's an answer for you.
BTW, if you use var_dump or var_export, you can see the whitespace :-).
$arr = preg_split('/(^\s+)/', " word1 word2 word3.", -1, PREG_SPLIT_DELIM_CAPTURE);
The ^ means beginning of string.
This will still give you an empty string in $arr[0], so do something like:
unset($arr[0]); $arr = array_values($arr);
Or just write your code to expect the extra entry. ;-)
-- paperCrane <Justin Patrin>
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php