2

I have string that contains area map and some other info:

$string = 'something here 
<map name="map">
< area shape="circle" coords="34,210,3" alt="something 1" href="test.php?place=aaa&time=1" />
< area shape="circle" coords="34,220,3" alt="something2" href="test.php?place=bbb&time=2" />
< area shape="circle" coords="669,229,3" alt="some 8" href="test.php?place=bbb&time=3" />
</map>';

How can I get coordinates, place and time extracted from string for each area?

user1829015
  • 57
  • 1
  • 6

2 Answers2

4

Try this out, I used regex to extract the variables into an array. You can see the results with the var_dump.

$string = 'something here 
<map name="map">
< area shape="circle" coords="34,210,3" alt="something 1" href="test.php?place=aaa&time=1" />
< area shape="circle" coords="34,220,3" alt="something2" href="test.php?place=bbb&time=2" />
< area shape="circle" coords="669,229,3" alt="some 8" href="test.php?place=bbb&time=3" />
</map>';

preg_match_all("/(\d+\,\d+\,\d+)/", $string, $coords);
preg_match_all("/place\=(.*)\&time\=(\d+)/i", $string, $place_time);

var_dump($place_time, $coords);

var_dump() results

array (size=3)
  0 => 
    array (size=3)
      0 => string 'place=aaa&time=1' (length=16)
      1 => string 'place=bbb&time=2' (length=16)
      2 => string 'place=bbb&time=3' (length=16)
  1 => 
    array (size=3)
      0 => string 'aaa' (length=3)
      1 => string 'bbb' (length=3)
      2 => string 'bbb' (length=3)
  2 => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)

array (size=2)
  0 => 
    array (size=3)
      0 => string '34,210,3' (length=8)
      1 => string '34,220,3' (length=8)
      2 => string '669,229,3' (length=9)
  1 => 
    array (size=3)
      0 => string '34,210,3' (length=8)
      1 => string '34,220,3' (length=8)
      2 => string '669,229,3' (length=9)

You would access the information in the above example by array/key:

echo $coords[1][0]; //return: 34,210,3
echo $place_time[1][0]; //return: aaa
echo $place_time[2][0]; //return: 1
phpisuber01
  • 7,585
  • 3
  • 22
  • 26
  • You can use `parse_url` and `parse_str` to parse the URL; you will then be able to successfully parse `test.php?time=1&foo=bar&place=aaa`. – Salman A Nov 29 '12 at 13:14
  • Regex + XML/HTML ===> EVIL. It's nearly as evil as using `eval` -> [Read this!](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) ASAP – Elias Van Ootegem Nov 29 '12 at 13:15
  • @EliasVanOotegem Fair enough, but were not matching html here, simply some arbitrary text that happens to be in tags, which even if they were missing would still work. Your solution is good too. Potato-Potato. – phpisuber01 Nov 29 '12 at 13:19
  • @phpisuber01: I beg to differ, this isn't just some arbitrary text - I'd say we're dealing with XML. The OP might shed some light on who's got the right idea here – Elias Van Ootegem Nov 29 '12 at 14:32
  • @Elias: stop quoting that answer, please. A string that contains `<` and `>` is not a HTML document. – Salman A Nov 29 '12 at 14:56
  • @SalmanA: Steady on - I'm just saying what I _think_. Just out of curiosity though: what answer did you think I was quoting? Or are you referring to the link to that Cthulu-thing? – Elias Van Ootegem Nov 29 '12 at 15:01
  • @Elias: The one the contains gibberish. It does not make sense to me. BTW, I have no objections about using DOMDocument, I just like to keep things simple. – Salman A Nov 29 '12 at 15:08
  • @SalmanA: That's the point of the guy who posted that answer: _it doesn't make sense_ in his (and my) opinion to use regex on markup. But everybody is entitled to their own opinion, and I understand where you're coming from. Happy Coding – Elias Van Ootegem Nov 29 '12 at 15:34
  • Hi! This solution works. Also, this has nothing to do with xml; I get that variable from database and it contains area map in addition to other information. – user1829015 Nov 30 '12 at 06:33
2

This looks like markup to me, why don't you simply parse it?

$DOM = new DOMDocument();
$DOM->loadXML($string);
$areas = $DOM->getElementsByTagName('area');
$coordinates = array();
for ($i = 0, $j = count($areas);$i<$j;$i++)
{
    array_push($areas, $areas[$i]->getAttribute('coords'));
}

That's the way you should treat markup, IMO - unless of course you're looking forward to the end of times

See how you can make your life easier when parsing markup, that's what the docs are for.

As @SalamanA pointed out, treating a fragment as an entire DOM could be considered overkill. Thankfully, there is such a thing as a DOMFragment class which can be used in just such a case.

$DOMFragment = new DOMDocumentFragment();
$DOMFragment->appendXML($string);
//or, when you need to treat multiple DOMFragments:
$DOM = new DOMDocument();//can be used as a sort-of DOMFragment factory
$fragment = $DOM->createDocumentFragment();
$fragment2 = $DOM->createDocumentFragment();
$fragment->appendXML($string);
Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • Kill a bird with cannon? While generally I do not endorse parsing HTML with regex, but using DOMDocument to parse fragments of HTML is sometimes an overkill. – Salman A Nov 29 '12 at 13:18
  • @SalmanA: Fair enough, I've edited my answer to point out the DOMDocumentFragment object – Elias Van Ootegem Nov 29 '12 at 14:29