I want to get all javascript, css and image sources from an url. I am using simple dom parse for this. This is my current code:
Load url:
$html = file_get_html($n);
Parse javascript sources:
foreach($html->find('script') as $element){
$value = $element->src;
if ($value != null){
if (strpos($value, '//') === 0) {
} else {
if(filter_var($value, FILTER_VALIDATE_URL)){
$array[] = strip_tags($value);
}
else {
$array[] = strip_tags($n.$value);
}
}
}
}
Parse image sources:
foreach($html->find('img') as $element){
$value = $element->src;
if ($value != null){
if (strpos($value, '//') === 0) {
} else {
if(filter_var($value, FILTER_VALIDATE_URL)){
$array[] = strip_tags($value);
}
else {
$array[] = strip_tags($n.$value);
}
}
}
}
Parse css sources:
foreach($html->find('link') as $element){
$value = $element->href;
if ($value != null){
if (strpos($value, '//') === 0) {
} else {
if(filter_var($value, FILTER_VALIDATE_URL)){
$array[] = strip_tags($value);
}
else {
$array[] = strip_tags($n.$value);
}
}
}
}
My only problem is with parsing css, i would like to use something like this - foreach($html->find('link rel="stylesheet"') as $element)
, but its not working that way. Is there a better (more accurate or appropriate) way to parse these things?