Help with PHP - preg_match ( get data from site via php )

Status
Not open for further replies.

Abhi788

New Member
4
2013
0
0
Hey Guys ,

I'm trying to get some data from a website via php and need some help with some coding.

i basically used : $content = file_get_contents('siteurl'); // to get data from the site

Part 1 *************************************************************************

I now need to shift through that data to get more specific items :

Data source is something like this:
<tr>
<th>Stock No</th><td>B37659</td>
<th>Chassis No</th><td>SC11-143781</td>
</tr>

<tr>
<th>Manufacturer</th><td>Nissan</td>
<th>Model</th><td>Tiida Latio 15B</td>
</tr>

<tr>
<th>Manuf. Year</th><td>2008</td>
<th>Registration Year</th><td>2008/10</td>
</tr>

<tr>
<th>Displacement</th><td>1490CC</td>
<th>Transmission</th><td>AUTO</td>
</tr>

<tr>
<th>Fuel Type</th><td>GASOLINE</td>
<th>Current Mileage</th><td></td>
</tr>


I want to get the details for the items e.g :

$fuel_type = $x ;
$x should return "Gasoline"

the code should read this line : <th>Fuel Type</th><td>GASOLINE</td> and return the 2nd option "Gasoline"

Like match this :
<th>Fuel Type</th>

and return its corresponding value


Part 2 *************************************************************************

Get url from 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 3 *************************************************************************

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>


Any help would be greatly appreciated . Thanks Guys
 
Last edited:
4 comments
So you have a list of headers to compare the search or do you want the code to find all headers and return the values after them?

What do you plan to do with the data? Store it in a database somewhere?

Number 2:

This assumes the webpage data is in $data varible and there is only one mainimage
Code:
$string_exp = '/<img(.*?)src=["|\'](.*?)["|\'](.*?)id="mainimage"/imx';
preg_match($string_exp,$data,$match);
$url = $match[2];

Number 3 has links as
Code:
[/FONT] images (<href ) and as images (<img ) [FONT=Verdana]
which is it? Or both?
 
Last edited:
I'm just a newb php coder but I guess this might work in your first problem:

1. Do a preg_match_all on every line like: "<th>Stock No</th><td>B37659</td>" to output a per line array.
2. Then another preg_match_all to separate "Stock No" and "B37659" to separate the two variables.
 
Preg_match function is easy you need to show the place and the tags where is the content wich you want to get.
For example:
PHP:
$data = file_get_contents("http://www.maestroweb.it");

echo $data;

Code:
<title>maestroweb - Realizzazione siti Internet</title>

PHP:
$data = file_get_contents("http://www.maestroweb.it");

preg_match('@<title>(.*?)</title>@si',$data,$title);

echo $title[1];
 
Last edited:
Status
Not open for further replies.
Back
Top