开发者

Xpath regex and preg-match function

I have this code:

<div class="small_map_wrapper">
                <script type="text/javascript">loadMap('44.7958873,20.471077899999954');</script>

I will fetch this content with:

$string = $xpath->query("//div[@class='small_map_wrapper']/@script")->item(0)->textContent;

then i try to get this numbers '44.7958873,20.471077899999954' with preg_match:

preg_match('/.*loadMap([\d.,]+)<.*/', $string, $matc开发者_StackOverflow中文版hes); 
list($lat, $lng) = explode(',', $matches[1]);
$data['lat'] = $lat; 
$data['lng'] = $lng;

BUT SOMETHING IN CODE I WROTE WRONG. I cant see error. Someone see error because after start this code $data['lat'] is 0, lng too (my english is not very well, sorry for that)


Instead of using a complicated regular expression, you can use a much easier to use function called sscanf:

$r = sscanf($str, "loadMap('%f,%f", $lat, $lng);

It allows you to directly parse those two float values into variables for the string given (Demo).


@script - this is wrong. You are trying to get an attribute called script, not the tag. Remove that @.

Your regex is also wrong, because it's looking for something like loadMap12.345,67.890<. Try this regex instead: /loadMap\('([0-9.,]+)'\)/

That should do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜