Get img src with PHP
Use a HTML parser like DOMDocument
and then evaluate the value you're looking for with DOMXpath
:
$html = '<img id="12" border="0" src="/images/image.jpg"
alt="Image" width="100" height="100" />';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//img/@src)"); # "/images/image.jpg"
Or for those who really need to save space:
$xpath = new DOMXPath(@DOMDocument::loadHTML($html));
$src = $xpath->evaluate("string(//img/@src)");
And for the one-liners out there:
$src = (string) reset(simplexml_import_dom(DOMDocument::loadHTML($html))->xpath("//img/@src"));
PHP get img src from xml
You have HTML embedded in XML tags, so you have to retrieve XML nodes, load each HTML and retrieve desired tag attribute.
In your XML there are different <description>
nodes, so using ->getElementsByTagName
will return more than your desired nodes. Use DOMXPath
to retrieve only <description>
nodes in the right tree position:
$dom = new DOMDocument();
libxml_use_internal_errors( True );
$dom->loadXML( $xml );
$dom->formatOutput = True;
$xpath = new DOMXPath( $dom );
$nodes = $xpath->query( 'channel/item/description' );
Then iterate all nodes, load node value in a new DOMDocument
(no need to decode html entities, DOM already decodes it for you), and extract src
attribute from <img>
node:
foreach( $nodes as $node )
{
$html = new DOMDocument();
$html->loadHTML( $node->nodeValue );
$src = $html->getElementsByTagName( 'img' )->item(0)->getAttribute('src');
}
eval.in demo
Assign Image SRC in php
Inside the HTML you still need to echo
the value of $src
. This should work:
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>
PHP - How to get all urls from many img's src?
Use preg_match_all():
$src = <<<EOL
Its really great to <img src="image2.png" /> hear from you "Today is good
<img src="http://www.google.com/picture2.png" /> day" Let's listen song
together! ---------<img src="images/profile.png" />\\\\\\
EOL;
preg_match_all('~src="([^"]+)~', $src, $matches);
var_export($matches[1]);
// output ->
// array (
// 0 => 'image2.png',
// 1 => 'http://www.google.com/picture2.png',
// 2 => 'images/profile.png',
// )
Live demo
Update: you can use \K
in the regex pattern to get just what is needed in $matches
:
preg_match_all('~src="\K[^"]+~', $src, $matches);
var_export($matches);
// output ->
// array (
// 0 =>
// array (
// 0 => 'image2.png',
// 1 => 'http://www.google.com/picture2.png',
// 2 => 'images/profile.png',
// ),
// )
For a reference see Escape sequences
Get img src with PHP Simple HTML DOM
You are having difficulty because javascipt is used to lazy load the image once the page is loaded. Use phpDom to find the Id of the element, and then use regular expression to find the relevant images based on this Id.
To achieve this, try something like :
$json = json_decode("<JSONSTRING HERE>");
foreach($html->find('div[class=profile_CF48B2B4A31B43EC96F0561F498CE6BF] a img') as $element) {
$imgId = $element->getAttribute('id');
foreach ($json as $lazy)
{
if ($lazy["id"] == $imgId) echo $lazy["data"];
}
}
The above is untested so you will need to resolve the kinks. They key is to extract the relevant javascript and convert it to json.
Alternatively, you can use string search functions to get the row which contains the information about the img, and extract the required value.
PHP get image src
You can use an html parser for this. See this one: http://simplehtmldom.sourceforge.net/ (I hope this works for nodes, not just for entire pages).
Related Topics
PHP Session Data Not Being Saved
Does PHP Time() Return a Gmt/Utc Timestamp
MySQLi Count(*) Always Returns 1
Is There a PHP Equivalent of Perl'S Www::Mechanize
Fatal Error: Class 'PHPmailer' Not Found
Setting the PHP Default Encoding to Utf-8
How to Check If an Array Contains a Specific Value in PHP
Checking If Customer Has Already Bought Something in Woocommerce
PHP Fatal Error: Call to Undefined Function Mssql_Connect()
Is MySQL_Real_Escape_String() Broken
Creating Jquery Ajax Requests to a PHP Function
How to Install Curl on Windows
Escaping Single Quote in PHP When Inserting into MySQL
How to Get Code Point Number For a Given Character in a Utf-8 String
PHP Variable in HTML No Other Way Than: ≪PHP Echo $Var; ≫
How to Validate Domain Name in PHP
What's the Difference Between 'Isset()' and '!Empty()' in PHP