resume resume - 3 months ago 8
PHP Question

how to handle this string and extract data out of it

$string='eDreams 459 Travel Up 464 AirAfrica 464 Available at 459 Available at 464 Available at 464 Available at 473 Available at 478 Available at 479 Available at 498';
how to get data like:
array =
0 => string 'eDreams' (length=6)
1 => string '459'
array=
0 => string 'Travel Up' (length=6)
1 => string '464'
array =
0 => string 'AirAfrica' (length=6)
1 => string '464'


how to do this i tried this with explode it works fine but not accurate help me please ?

Answer

I found a way.. it's not pretty. I hope it helps you.

<?php
$string = "eDreams 459   Travel Up 464   AirAfrica 464   Available at  459  Available at  464  Available at  464  Available at  473  Available at  478  Available at  479  Available at  498";
$array = explode(" ", $string);
$final_array = array();
$found = false;
foreach ($array as $value) {
    if (ctype_digit($value)){
        $final_array[count($final_array)-1][] = (int)$value;
        $found = false;
    } else {
        if ($found == false){
            $final_array[] = array($value);
            $found = true;
        } else {
            $final_array[count($final_array)-1][0] .= " " . $value;
        }
    }
}
print_r($final_array);
?>

I wouldn't use something like this. Maybe you can get a better result from db directly.

Comments