0

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]);

?>
4
  • What exactly isn't working? Commented Nov 14, 2014 at 3:59
  • Just trying to figure out to extract the coordinates. Commented Nov 14, 2014 at 4:00
  • Hes asking how to extract the valuse 45.5367, and -122.95596 using PHP DOM parser. Its possible, using preg match. stackoverflow.com/questions/19471285/… Commented Nov 14, 2014 at 4:00
  • I just ran it and it worked perfectly. Do you have the included file in the same directory? Try running it at the command line (if you aren't already) and tell us what you see. (To run at the command line, you do php nameofyourfile.php.) Commented Nov 14, 2014 at 4:29

2 Answers 2

1

Try followed regexp:

preg_match_all(/parseFloat\(([^\)]+)/, $js, $matches);
print_r($matches[1]);
Sign up to request clarification or add additional context in comments.

2 Comments

This returns $matches[0] = parseFloat( $matches[1] = 4. sandbox.onlinephpfunctions.com/code/…
thanks, added forgetten +, and you use preg_match in your fiddle instead of preg_match_all in my answer
0

Try 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

I understand how to do this part, its traversing the DOM elements to find the "LoadMarker..." text in the first place.
Are they not just inside script elementS?
@Jon You should try to be more specific in your question. Please add code to show us what you've done so far. Guessing at your problem when we have no code is a waste of time (for you as well).
They are, but I am having a hard time getting to this specific script element.
Well, you should rephrase the question, because your question asks how to get the Coordinates. Not how to get the script tags
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.