way after str_replace function $img is empty and secont $img->find('img') function show error:
Fatal error: Cannot use object of type simple_html_dom as array in D:\wamp\www\test.php on line 7
<?php
require_once('simple_html_dom.php');
$img_html = str_get_html('hhtml tekst html tekst <img src = "img.png" /> ad sad');
foreach($img_html->find('img') as $element)
$img[] = $element->src . '<br>';
$img_html = str_replace($img[0], 'n-'.$img[0], $img_html);
foreach($img_html->find('img') as $element2)
echo $element2->src . '<br>';
?>
Is this really what you're aiming to do?
foreach($img->find('img') as $element)
$img[] = $element->src . '<br>';
$img is an object on the first foreach -loop, but then you're trying to access $img as an array, which is causing the error. Are you accidentally using the same variable name for two different things?
You have to put str_get_html before str_replace as:
$img_html = str_get_html('hhtml tekst html tekst <img src = "img.png" /> ad sad');
foreach($img_html->find('img') as $element)
$img[] = $element->src;
$img_html = str_get_html(str_replace($img[0], 'n-'.$img[0], $img_html));
foreach($img_html->find('img') as $element2)
echo $element2->src . '<br>';
Also check that you have given correct path in require_once( 'simple_html_dom.php');
Related
I'm new to PHP and I would like to know how to retrieve data from an HTML element such as an src?
It's very easy to do that in jQuery:
$('img').attr('src');
But I have no idea how to do it in PHP (if it is possible).
Here's an example I'm working on:
I loaded $result into SimpleXMLElement and stored it into $xml:
$xml = simplexml_load_string($result) or die("Error: Cannot create object");
Then used foreach to loop over all elements:
foreach($xml->links->link as $link){
echo 'Image: ' . $link->{'link-code-html'}[0] . '</br>';
// returns sometihing similar to: <a href='....'><img src='....'></a>
}
Inside of the foreach I'm trying to access links (src) in img.
Is there a way to access src of the img nested inside of the a — clear when outputted to the screen:
echo 'Image: ' . $link->{'link-code-html'}[0] . '</br>';
I would do this with the built-in DOMDocument and DOMXPath APIs, and then you can use the getAttribute method on any matching img node:
$doc = new DOMDocument();
// Load some example HTML. If you need to load from file, use ->loadHTMLFile
$doc->loadHTML("<a href='abc.com'><img src='ping1.png'></a>
<a href='def.com'><img src='ping2.png'></a>
<a href='ghi.com'>something else</a>");
$xpath = new DOMXpath($doc);
// Collect the images that are children of anchor elements
$imgs = $xpath->query("//a/img");
foreach($imgs as $img) {
echo "Image: " . $img->getAttribute("src") . "\n";
}
AA Dear bro, i want to get the img src from a html page butt i have faced with error,Help please , my server show this messaage
Notice: Undefined offset: 0 in F:\xamppppp\htdocs\Arslan_Sir\img
download from google.php on line 13 Notice: Array to string
conversion in F:\xamppppp\htdocs\Arslan_Sir\img download from
google.php on line 15 Array
my code is
<?php //this code can be pic
image from a html page $ctual_link="https://www.google.com/search?q=9780333993385&ie=utf-8&oe=utf-8&client=firefox-b-ab"; define('DIRECTORY', '/imgg/m/'); $text = file_get_contents($ctual_link); preg_match_all('/<div class=\"image\">(.*?)<\/div>/s', $text, $out); //preg_match('/~src="(.*)"itemprop="image" \/>/',$text,$out); preg_match('~src="(.*)"\s*itemprop="image"[^>]*>~',$text,$out); //$out
= explode(' ',$out[1]); $z=trim($out[0],'"'); echo $out; //} ?>
Not quite sure but thinking about PHP Simple HTML DOM Parser
the example from the landing page of the library
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
solved your issue you can apply this code it will help you better
<?php
$ctual_link="https://www.google.com/search?q=9780333993385&ie=utf-8&oe=utf-8&client=firefox-b-ab";
$html = file_get_contents($ctual_link);
//Create a new DOM document
$dom = new DOMDocument;
#$dom->loadHTML($html);
$links = $dom->getElementsByTagName('img');
foreach ($links as $link){
//Extract and show the "src" attribute of image.
echo $link->nodeValue;
echo $link->getAttribute('src'), '<br>';
}
?>
foreach($divs as $element)
{
$img = $element->find('a', 0)->href.'<br>';
$img = str_replace("http://www.youtube.com/watch?v=", "http://img.youtube.com/vi/", $img);
echo $img."/mqdefault.jpg";
}
Now output is :
http://img.youtube.com/vi/UYkHcfrxxtk
/mqdefault.jpghttp://img.youtube.com/vi/ZgjaveCUsSg
/mqdefault.jpghttp://img.youtube.com/vi/TFVbehwl1Ns
/mqdefault.jpg
I want like this each img:
http://img.youtube.com/vi/UYkHcfrxxtk/mqdefault.jpg
How can get fix this? i used simple html dom.
echo $img."/mqdefault.jpg<br/>";
But if you are trying to make image elements on a page:
echo "<img src='".$img."' /><br/>";
use this.
remove .'<br>' from end of $img = $element->find('a', 0)->href.'<br>'; then add it to echo $img."/mqdefault.jpg";
<br> in html mean newline
$img = $element->find('a', 0)->href;
$img = str_replace("http://www.youtube.com/watch?v=", "http://img.youtube.com/vi/", $img);
echo $img."/mqdefault.jpg".'<br>';
I'm trying to learn the simple_html_dom syntax, but i'm not having much luck. Could someone show me an example from this:
<div id="container">
<span>Apples</span>
<span>Oranges</span>
<span>Bananas</span>
</div>
If I want to just return the values Apples, Oranges and Bananas.
Can I simply use the php simple_html_dom class or will I also have to use xcode, curl, etc?
UPDATE:
I was able to get this to work, but not convinced it's the most efficient way of getting what I need:
foreach ($html->find('div[id=cont]') as $div);
foreach($div->find('span') as $element)
echo $element->innertext . '<br>';
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
Your suggestion is correct:
foreach ($html->find('div[id=cont]') as $div);
foreach($div->find('span') as $element)
echo $element->innertext . '<br>';
More simply:
foreach($html->find('div#container span') as $element)
echo $element->innerText();
That means any span that descends from a div with id: container
I have a php string that contains the below HTML I am retrieving from an RSS feed. I am using simple pie and cant find any other way of splitting these two datasets it gets from <description>. If anyone knows of a way in simple pie to select children that would be great.
<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div>
to:
$image = '<img title="example" alt="example" src="example.jpg">';
$description = 'EXAMPLE TEXT';
$received_str = 'Your received html';
$html = str_get_html($received_str);
//Image tag
$img_tag = $html->find("img", 0)->outertext;
//Example Text
$example_text = $html->find('div[style=example]', 0)->last_child()->innertext;
See Here: http://simplehtmldom.sourceforge.net/manual.htm
Try Simple HTML Dom Parser
// Create DOM from HTML string
$html = str_get_html('Your HTML here');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Description
$description = $html->find('div[style=example]');
try using strip_tags:
<?php
$html ='<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div>';
$html = strip_tags($html,'<img>');
// $html == '<img title="example" alt="example" src="example.jpg">'
?>