user2984982
user2984982

Reputation: 13

PHP - Get data from site using ID or class

Its been a while since i learned php and kind of forgot how to get the url using the id & class .

Any idea how to get the $img = "url.jpg" using the id (mainimage) from the site.

<img style="width:100%;" src="url.jpg" id="mainimage"/><div class="clearfix"></div>

Part 2

Also if there are more than one image in the same class e.g :

$thumb1 = "image_url1";
$thumb2 = "image_url2";

<div id="gallery"><a href="url/stock_img2/36536/1.jpg" class="gallary-tmb"><img src="url/stock_img2/36536/1.jpg" alt="TIIDA LATIO 15B" /></a><a href="url/stock_img2/36536/2.jpg" class="gallary-tmb"><img src="url/stock_img2/36536/2.jpg" alt="TIIDA LATIO 15B" /></a></div><div class="clearfix"></div>
</div>

Upvotes: 0

Views: 697

Answers (3)

Hango
Hango

Reputation: 26

You can use preg_match() to get url.

Similarly, you can modify the pattern to search for results of your interest.

Try:

preg_match('/src\="(.*)"\sid\="mainimage"/', $test, $match);
$url = $match[1];

preg_match('/href\="(.*)"\sclass\="gallary\-tmb"/', $test, $match);
$thumb1 = $match[1];

BTW, removing the original question could make some of the great answers seem irrelevant. If could please add the original question back in the question area and vote on the previous answers.

Upvotes: 1

Nick M
Nick M

Reputation: 1666

If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches1 will have the text that matched the first captured parenthesized subpattern, and so on.

preg_match()

So you will need to be looking at $match1 at least, but I suggest you just use print_r($match) until you can find what you are doing.

Either way you capture groups in PHP with parentheses ().

So try;

$test = "<th>Fuel Type</th><td>GASOLINE</td>";
preg_match('/<td\>([a-z]*)\<\/td\>/i', $test, $match);

echo $match1;

Upvotes: 0

Machavity
Machavity

Reputation: 31644

You have to bound matches you want in parenthesis

preg_match('/\<th\>Fuel Type\<\/th\>\<td\>(.*)\<\/td\>/i', $test, $match);

Upvotes: 0

Related Questions