I am utilizing the HTML DOM Parser for PHP and am having difficult trying to extract the coordinates out of this javascript. Any clue how to? Thanks.
<script type="text/javascript">
//<![CDATA[
LoadMarker(parseFloat(45.5364416896354),parseFloat(-122.959525248125), 'seizures', '141101942', 'wccca');
LoadMarker(parseFloat(45.3885251463509),parseFloat(-122.813856693134), 'cardiac arrest', '141101935', 'wccca');
resizeIncidentScrollBar('#wccca-incidents');
LoadMarker(parseFloat(45.3926967266394),parseFloat(-122.622226603465), 'chest pain', '141101208', 'ccom');
LoadMarker(parseFloat(45.266375649134),parseFloat(-122.676613858032), 'hemorrhage', '141101206', 'ccom');
LoadMarker(parseFloat(45.0866856873256),parseFloat(-122.667733219612), '*m82', '141101198', 'ccom');
resizeIncidentScrollBar('#ccom-incidents');
updateMarkers();
Sys.Application.initialize();
Sys.Application.add_init(function() {
$create(Sys.UI._Timer, {"enabled":true,"interval":2000,"uniqueID":"tmrIncidents"}, null, null, $get("tmrIncidents"));
});
//]]>
</script>
PHP:
<?php
include 'simple_html_dom.php';
// Get WCCCA's html file
$html = file_get_html('http://www.wccca.com/PITSv2/');
$node = $html->find('script[type=text/javascript]', 4);
$DOMelement = array("LoadMarker(parseFloat(45.5364416896354),parseFloat(-122.959525248125)");
preg_match_all('/\d+(\.\d+)?/', $DOMelement[0], $matches);
print_r($matches[0]);
?>
2 Answers 2
Try followed regexp:
preg_match_all(/parseFloat\(([^\)]+)/, $js, $matches);
print_r($matches[1]);
2 Comments
+, and you use preg_match in your fiddle instead of preg_match_all in my answerTry using preg_match
preg_match ($pattern, $DOMelement, $matches);
Pattern to look for floats/coordinates.
'/\d+(\.\d+)?/'
So basically like this
// Simulation of your DOM element
$DOMelement = array("LoadMarker(parseFloat(45.5364416896354),parseFloat(-122.959525248125)");
preg_match_all('/\d+(\.\d+)?/', $DOMelement[0], $matches);
returns
[0] => Array
(
[0] => 45.5364416896354
[1] => 122.959525248125
)
Example here...
http://sandbox.onlinephpfunctions.com/code/d026735963c62fc532d3d6442dfdebc14ca580b1
Although it will also find decimals, of those floats. Which is not ideal. But the values you want will be in the 1st index. As you can see in the example link
Based on your update...
If the script is in the same spot on the page all the time
$html = file_get_html('http://www.wccca.com/PITSv2/');
$node = $html->find('script', 0);
preg_match_all('/\d+(\.\d+)?/', $node->nodeValue;, $matches);
7 Comments
Explore related questions
See similar questions with these tags.
php nameofyourfile.php.)