$string = '<img alt="Peachtree St Apartment 2" data-url="http://i.oodleimg.com/item/3472578127t_1m_condos,_townhouses_&_apts_for_sale_in_johnson_city_tn/?1377909664" src="http://i.oodleimg.com/a/animate/spinner-small.gif" class="lazyload-image">';
I want to extract the data-url parameter in PHP.
This is a great job for XPath! Don't know what XPath is? Let Wikipedia answer that:
XPath, the XML Path Language, is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. XPath was defined by the World Wide Web Consortium (W3C).
So for your specific user case, the query will be:
//img[@class='lazyload-image']/@data-url
So you can use it like:
$query("//img[@class='lazyload-image']/@data-url");
$xpath->query($query);
Then you're free to iterate over it. I've taken the class appended to your img-tag into consideration in this matter, but feel free to remove that:
//img/@data-url
More info on how to use XPath with PHP (DOMXPath) in the manual
Using regular expressions you can do the following:
$string = '<img alt="Peachtree St Apartment 2" data-url="http://i.oodleimg.com/item/3472578127t_1m_condos,_townhouses_&_apts_for_sale_in_johnson_city_tn/?1377909664" src="http://i.oodleimg.com/a/animate/spinner-small.gif" class="lazyload-image">';
$matches = array();
$url = '';
if (preg_match('/data-url="([^"]+)"/', $string, $matches)) {
$url = $matches[1];
}