phpexplode

How to split text which has spaces and newlines


I'm getting below string by using cURL. The code can be found in this comment.

HTTP/1.1 200 OK ID=347 Date: Tue, 19 Feb 2013 09:15:25 GMT Server:
Apache/2.2.22 (Ubuntu) Content-length: 0 Vary: Accept-Encoding
Content-Type: text/plain; charset=ISO-8859-1

Now I want ID from the string and for that I've used explode to make an array from the space like below:

$getIdElem = explode(' ',$output);
echo '<pre>';print_r($getIdElem);

but I'm getting Date: with ID string while its having space before it. Output of printing an array is:

Array
(
    [0] => HTTP/1.1
    [1] => 200
    [2] => OK
    [3] => ID=347
Date:
    [4] => Tue,
    [5] => 19
    [6] => Feb
    [7] => 2013
    [8] => 09:15:25
    [9] => GMT
Server:
    [10] => Apache/2.2.22
    [11] => (Ubuntu)
Content-length:
    [12] => 0
Vary:
    [13] => Accept-Encoding
Content-Type:
    [14] => text/plain;
    [15] => charset=ISO-8859-1
)

I don't understand why it doesn't include that space in explode function.


Solution

  • Use a regex instead:

    if (preg_match('# ID=(\d+) #', $response, $match)) { 
       $id = $match[1];
    }