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>
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;
?>
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;
$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');
}
精彩评论