Reputation: 51
<p class="my-image my-image-zoom">
<div id="wrap" style="top:0px;z-index:9999;position:relative;"><a href="http://img3.mysite.com/image.jpg" class="cd-zoom" id="prozoom" rel="adjustX: 5, zoomWidth:526, zoomHeight:440, adjustY:-1" style="position: relative; display: block;">
<img width="400" height="440" id="image" src="http://img3.mysite.com/image.jpg" style="display: block;"></a><div class="mousetrap" style="background-image: url(http://www.mysite.com/); z-index: 999; position: absolute; width: 400px; height: 440px; left: 0px; top: 0px; cursor: move;"></div></div>
</p>
i tried with
preg_match_all('/product-image-zoom">(.*?)/s',$url,$sav);
print_r($sav);
i like to clip the image source. eventually i start from getting all values from class name. i dont find my code works. can any one help me in getting the source of image?
Upvotes: 0
Views: 1085
Reputation: 76666
I'd advise against using regex for parsing HTML. Here's an alternative solution:
<?php
$html= '<p class="my-image my-image-zoom">
<div id="wrap" style="top:0px;z-index:9999;position:relative;"><a href="http://img3.mysite.com/image.jpg" class="cd-zoom" id="prozoom" rel="adjustX: 5, zoomWidth:526, zoomHeight:440, adjustY:-1" style="position: relative; display: block;">
<img width="400" height="440" id="image" src="http://img3.mysite.com/image.jpg" style="display: block;"></a><div class="mousetrap" style="background-image: url(http://www.mysite.com/); z-index: 999; position: absolute; width: 400px; height: 440px; left: 0px; top: 0px; cursor: move;"></div></div>
</p>';
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//img/@src)");
echo $src; //output: http://img3.mysite.com/image.jpg
?>
Codepad: http://codepad.org/C4oKp4LI
Hope this helps!
Upvotes: 1
Reputation: 2499
While using regex is not recommended to parse html, i would recommend doing something like:
$dom = new DOMDocument('1.0', 'utf-8');
@$dom->loadHTML($html);
$xpath_query = "//img";
$xpath = new DOMXPath($dom);
$xpath_query_results = $xpath->query($xpath_query);
foreach($xpath_query_results as $result)
{
$src = $result->getAttribute('src');
print_r($src);
}
then you can test the $src
variable against your regex :)
Upvotes: 1
Reputation: 93805
Don't use regular expressions to parse HTML. You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged.
Upvotes: 0