0

i'm having a problem parsing a url. i would like to do it with dom but ihave no idea where to start. i want to only parse the src from the following code. any ideas would be great thank you.

<script type="text/javascript" src="http://website.com/?code-ajax&cd=1145040425"></script>

3 Answers 3

1

Using the SimpleHTMLDom Library:

<?php
    // include the SimpleHTMLDom library
    include('lib/simple_html_dom.php');

    // our input string
    $input = '<script type="text/javascript" src="http://website.com/?code-ajax&cd=1145040425"></script>';

    // create our Object. If reading from a file, use: file_get_html('/path/to/file');
    $doc = str_get_html($input);

    // find the first <script> tag, and echo its 'src' attribute.
    // -- note: calling this with find('script') returns an array
    echo $doc->find('script',0)->src;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

It's one of the best things in PHP, along with Hip Hop
1

My favorite solution is simplehtmldom php library, it is alot simpler to use than PHP's native solution; I say this from experience. It's similar to jQuery in it's syntax and usage.

You may use it like this

include('lib/simple_html_dom.php');
$html = str_get_html('<script type="text/javascript" src="http://website.com/?code-ajax&cd=1145040425"></script>');
$scriptsrc = $html->find('script',0)->src;

Comments

1
$doc = new DOMDocument();
$doc->loadXML($xml);
$src = $doc->documentElement->getAttribute('src');

XPath example using remote file

$doc = new DOMDocument();
$doc->loadHTMLFile($url);
$xpath = new DOMXpath($doc);

// Retrieve collections of script nodes
$allScripts  = $xpath->query('//script');
$headScripts = $xpath->query('/html/head/script');
$bodyScripts = $xpath->query('/html/body/script');

// Get all scripts who's src attribute starts with "http://website.com"
$websiteScripts = $xpath->query('//script[starts-with(@src, "http://website.com")]');
if ($websiteScripts->length) {
    // contains one or more matches
    $src = $websiteScripts->item(0)->getAttribute('src');
}

7 Comments

Weird, can't post anything containing the script tag (corporate firewall maybe). Assume the $xml variable contains the string in your question
@PhilBrown what if i wanted to use this for an external webpage
@sabrina You can use DOMDocument::loadXMLFile() or DOMDocument::loadHTMLFile() depending on the doctype of the page. You would also need to change how you reach the script node as you would be dealing with an entire page and not just one node. You should probably edit your question to better reflect what you're trying to do
@PhilBrown i tried this but returns a blank page <?php libxml_use_internal_errors(TRUE); $dom = new DOMDocument; $dom->loadHTMLFile($url); libxml_clear_errors(); $xp = new DOMXpath($dom); $nodes = $xp->documentElement->getAttribute('src'); ?>
@sabrina That's not how you use an XPath query. I'll edit my answer to show you an example
|

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.