[thelist] condensing 4 regexes to 1?

Alex Beston alex.beston at gmail.com
Sun Aug 6 12:28:36 CDT 2006


ta der.

been doing a bit more and ive discovered it is indeed possible to
shorten the code:

$string = ",viewport: {center: {lat: 57.138801,lng: -2.120481},span:
{lat: 0.002700,lng: 0.004975},mapType: null},overlays: {markers: [{id:
'A',lat: 57.138801,lng: -2.120481,image: '/mapfiles/arrow.png',icon:
function() { return parent.G_ADDR_ICON; },eid:
'ACPWRI2gLsqEswG1peiwDQ',inf";

$pattern = "/(center: {lat: )(-?[0-9]{1,3}\.[0-9]{6})(,lng:
)(-?[0-9]{1,3}\.[0-9]{6})/";

preg_match_all($pattern, $string, $matches, PREG_SET_ORDER);

print_r($matches);

Array
(
    [0] => Array
        (
            [0] => center: {lat: 57.138801,lng: -2.120481
            [1] => center: {lat:
            [2] => 57.138801 -- bingo!
            [3] => ,lng:
            [4] => -2.120481 -- bingo!
        )

)

however, the backreferencing would be good to have:

$pattern = "/(center: {lat: )(-?[0-9]{1,3}\.[0-9]{6})(,lng: )\2/";

but doesnt seem to work (ie i want to make a second use of
(-?[0-9]{1,3}\.[0-9]{6})

any ideas?

ta!

Alex



More information about the thelist mailing list