Reputation: 13
Using PHP Simple HTML DOM Parse but unable to get images to display.
I am not a coder and am trying to pull articles and images from a website. The articles are fine but the images are not displaying. Instead part of the path displays e.g.
> //ssl.gstatic.com/ui/v1/button/search-white.png
> //ssl.gstatic.com/ui/v1/button/search-white.png
> //ssl.gstatic.com/ui/v1/icons/common/settings.png
Using Google as an example, here's the code I am using:
<?php
$html = file_get_html('https://news.google.com/nwshp?hl=en&tab=in');
foreach($html->find('h2') as $e)
echo $e->innertext . '<br><br>';
foreach($html->find('div.jsdisplay') as $e)
echo $e->innertext . '<br>';
foreach($html->find('img') as $element)
echo $element->src . '<br>';
?>
Thanks for any help
Upvotes: 1
Views: 1973
Reputation: 7762
Update my answer after your last comment with your original site URL 'http://frielatvsales.com/QuadAttachments.htm'
try below code.
include_once "simplehtmldom/simple_html_dom.php";
$url = "http://frielatvsales.com/QuadAttachments.htm";
$html = file_get_html($url);
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1];
foreach($html->find('h2') as $e) {
echo $e->innertext . '<br><br>';
}
foreach($html->find('div.jsdisplay') as $e) {
echo $e->innertext . '<br>';
}
foreach($html->find('img') as $element) {
echo '<img src=http://'.$host.'/'.$element->src . ' /><br>';
}
Upvotes: 0
Reputation: 95101
You should replace
foreach($html->find('img') as $element)
echo $element->src . '<br>';
With
foreach ( $html->find('img') as $element ) {
$img = str_replace(array("//ssl"), array("http://ssl"), $element->src);
for($i = 0; $i < 5; $i ++) {
$img = str_replace("//nt$i", "http://nt$i",$img);
}
echo "<img src=\"$img\" /> <br>";
}
Upvotes: 2
Reputation: 943142
//ssl.gstatic.com/ui/v1/button/search-white.png
is a relative URI (the scheme is not specified, so it will use the same scheme (e.g. http: or https:) as the page it appears in).
Resolve it as you would any other relative URI.
My question is how to get the images to display using the code in my original post.
You have to output an <img>
tag instead of the URI as plain text.
Upvotes: 0