-4

I want to split this string up into parts like "Source: Web", "Pics: 1" ... to use it within my website. From the "Lat:" and "Lon:" I need to extract just the numbers.

    <cap>
Source: Web | Pics: 1 | Frame: 2 | Date: 4-25-2011 | On: App | Lat: 51.2222 | Lon: 7.6555
</cap>

What's the best way to do it? I read about explode() but I don't get it to work. Cheers

Michael
  • 327
  • 2
  • 6
  • 12
  • Maybe you should clarify whether you mean the string includes all the shown text, including tags, or only the inner text inside the tags, as some are taking the tags into account and some do not. – Jürgen Thelen Apr 25 '11 at 19:19
  • 4
    Come on. Using the title of your post I get a [ton of answers to your question](http://stackoverflow.com/search?q=php+split). Also, go back and accept some previous answers or cease asking them. – Kevin Peno Apr 25 '11 at 19:20

8 Answers8

4

Here is a bit of code I whipped up using explode (DEMO)

<?php    
    $str = "Source: Web | Pics: 1 | Frame: 2 | Date: 4-25-2011 | On: App | Lat: 51.2222 | Lon: 7.6555";
    $arr = explode(" | ", $str);
    foreach ($arr as $item){
        $arr2 = explode(": ", $item);
        $finalArray[$arr2[0]]=$arr2[1];
    }
    print_r($finalArray);
?>

RESULT

Array
(
    [Source] => Web
    [Pics] => 1
    [Frame] => 2
    [Date] => 4-25-2011
    [On] => App
    [Lat] => 51.2222
    [Lon] => 7.6555
)

USAGE

echo $finalArray['Lon']; //yields '7.6555'
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
1

Here's a ridiculous one-liner that should probably never be used, but it uses no loops (i hate loops). I also like to practice my REs

$str = 'Source: Web | Pics: 1 | Frame: 2 | Date: 4-25-2011 | On: App | Lat: 51.2222 | Lon: 7.6555';
preg_match( sprintf( '~%s~', implode(array_map( function($val){ if ($val) return sprintf( '%1$s:\s(?P<%1$s>.*?)(?:(?:\s\|\s)|(?:$))', $val ); }, preg_split( '~:.*?(?:(?:\s\|\s)|(?:$))~', $str ) ) ) ), $str, $m );
print_r($m);

result

Array
(
    [Source] => Web
    [Pics] => 1
    [Frame] => 2
    [Date] => 4-25-2011
    [On] => App
    [Lat] => 51.2222
    [Lon] => 7.6555
)
Galen
  • 29,976
  • 9
  • 71
  • 89
0
$pieces = explode(' | ','Source: Web...'); //Rest of string in there.
$items = array();
foreach ($pieces as $piece) {
    $parts = explode(': ', $piece);
    $items[$parts[0]] = $parts[1];
}
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0
$string = "Source: Web | Pics: 1 | Frame: 2 | Date: 4-25-2011 | On: App | Lat: 51.2222 | Lon: 7.6555";
$pieces = explode("|", $string);
print_r($pieces);
vicTROLLA
  • 1,554
  • 12
  • 15
0
$items = explode(' | ', "Source: Web | Pics: 1 | Frame: 2 | Date: 4-25-2011 | On: App | Lat: 51.2222 | Lon: 7.6555");

foreach ($items as $item) {
   $new_data = explode(': ', $item);
   $my_array[$new_data[0]] = $new_data[1];
}

print_r($my_array);
Mike
  • 16,690
  • 2
  • 19
  • 25
0

Try this one. It will split them and create an associative array:

$string = 'Source: Web | Pics: 1 | Frame: 2 | Date: 4-25-2011 | On: App | Lat: 51.2222 | Lon: 7.6555';
$list = explode('|', $string);
$assoc = array();
foreach($list as $part) {
    list($key, $val) = explode(':', $part);
    $assoc[trim($key)] = trim($val);
}

print_r($assoc);
ANisus
  • 74,460
  • 29
  • 162
  • 158
0
$val =<<<END
<cap>
    Source: Web | Pics: 1 | Frame: 2 | Date: 4-25-2011 | On: App | Lat: 51.2222 | Lon: 7.6555
</cap>
END;

$bits = split("[:|]", $val);
$lat = trim($bits[11]);
$lon = trim($bits[13]);
Femi
  • 64,273
  • 8
  • 118
  • 148
  • This will give you an array mixed with Keys **and** values. Poor form. – Dutchie432 Apr 25 '11 at 19:16
  • But +1 for using Heredoc syntax ^^ – Jürgen Thelen Apr 25 '11 at 19:21
  • How does that contribute at all to the question :) – Dutchie432 Apr 25 '11 at 19:41
  • It _may_ contribute s/t, because it's the only answer taking the tags into account by now. It's not clear whether the OP wanted to split inclusive or exclusive tags. But I of course agree that the splitting part is not, what I'd do^^ – Jürgen Thelen Apr 25 '11 at 20:00
  • Ah, @Dutchie432, did you actually run the code: it returns this: array(14) { [0]=> string(16) " Source" [1]=> string(5) " Web " [2]=> string(5) " Pics" [3]=> string(3) " 1 " [4]=> string(6) " Frame" [5]=> string(3) " 2 " [6]=> string(5) " Date" [7]=> string(11) " 4-25-2011 " [8]=> string(3) " On" [9]=> string(5) " App " [10]=> string(4) " Lat" [11]=> string(9) " 51.2222 " [12]=> string(4) " Lon" [13]=> string(14) " 7.6555 " }. All he cared about (from his question) is extracting lat/lon: the prettiness of mixed key/values seems kinda secondary. – Femi Apr 25 '11 at 20:47
  • 1
    I don't need to run the code, I can tell (as I previously stated) exactly what you will end up with. Who wants to retrieve their values by alternating numerical values? Seems that you're making much more work for the coder. `$array['lon']` is MUCH easier than `$array[11]` unless you can remember what each number represents. Again, I will restate, this is NEVER a preferable method. – Dutchie432 Apr 25 '11 at 20:52
  • Well, if you truly want to burn cycles, you can always build an array with string keys, but that just seems excessive: $j = count($bits); for($i=0;$i<$j;$i+=2) $final[$bits[$i]] = $bits[$i+1]; $lat = trim($final['lat']); $lon = trim($final['lon']); – Femi Apr 25 '11 at 21:38
-1

You are correct. explode is the best function for this.

Yada
  • 30,349
  • 24
  • 103
  • 144
  • 1
    This really isn't a good answer. From his question, he knows about explode() but couldn't get it to function properly. If you're going to suggest that he use it, you should provide an example, as did every other answer. ;) – BraedenP Apr 25 '11 at 19:10
  • Thank you guys for the many examples and infos. I really appreciate it! – Michael Apr 26 '11 at 00:26