faqts : Computers : Programming : Languages : PHP : Common Problems : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 31 people (58%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How do I split() a string at unquoted whitespace?

Dec 13th, 2002 01:14
Philip Olson, Del Rudolph,


The following:
<?php
    $string = "Hello        there friend    bye!";
    $arr = split('[ ]+', $string);
?>
Provides:
Array
(
    [0] => Hello
    [1] => there
    [2] => friend
    [3] => bye!
)
But if you did:
<?php
$string = "Hello        there friend    bye!";
$arr = explode(' ', $string);
?>
You'll get:
Array
(
    [0] => Hello
    [1] =>
    [2] =>
    [3] =>
    [4] =>
    [5] =>
    [6] =>
    [7] =>
    [8] => there
    [9] => friend
    [10] =>
    [11] =>
    [12] =>
    [13] => bye!
)