Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

PHP - Split string

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...