This is a sample of the string of data that i need to extract the price of.

"Price Rs 475 000 - CHEVROLET AVEO LS // SEP 11 6,000 kms - Red.. full options.. MANUAL 5 door // hatchback Contact me on 786 8394"

I have many such strings after crawling a particular website and there can be any number or words in the string.

I have tried to separate each word by a space and store it in the array $arr. I have declared another array for storing identifiers of price $arrPrice. if the word price or rs is found,then the data (eg 475 000) is stored in the variable $price. However as i have exploded it with space, it is not taking into consideration the 000 . I am getting only 475 in the xml tag.

The efficient way of doing it may be with regex,but i am not good at it. Grateful if someone could help me.

Find Below my codes till now,

Thanks!

<?php


    foreach($html->find('div.field-content') as $e) {//find the h3 element that contains class field content


    $arrPrice = array("rs", "price","rs."); // an array of identifiers to retrieve price

    $str = $e->innertext;// crawled data from a website
    $str = strtolower($str); //converting string to lower case
    $arr = explode(" ", $str);//creating an array of the string by seperating it from the spaces

    if (strlen($str) > 0) {
        $price='';

        for ($i = 0; $i < sizeof($arr); $i++) {

            //finding price 
            for ($j = 0; $j < sizeof($arrPrice); $j++) {
                if ($arr[$i]==$arrPrice[$j]) {
                    $price = $arr[$i+1];
                    //echo 'Price='.$arr[$i+1];

                }
            }   

        }
        $xml.="<Cars>";
        $xml.="<Price>".$price."</Price>";
        $xml.="</Cars>";
    } 

    else {
        echo "String is blank";
    }


}

$file = fopen('data.xml','w');
if(!$file) {
    die('Error cannot create XML file');
}
fwrite($file,$xml);
fclose($file);
?>

Recommended Answers

All 8 Replies

get html code of the url, paste it here, then we can try some regex on it for price

Honda Civic 150i,locale, manuelle. 88000 kms . Excellent Etat - Tel.:705 16 36

MITSUBISHI LANCER YEAR 00 FULL OPTION MOTEUR DOHC INJECTION BODY KIT, JANTE COSMIC POUR PLUS DE RESEIGNEMENT TEL 7787425 SERIOUS BUYER ONLY

Price Rs 475 000 - CHEVROLET AVEO LS // SEP 11 6,000 kms - Red.. full options.. MANUAL 5 door // hatchback Contact me on 786 8394

Couleur grise,2 portes,2 places, manuel, jantes AMG, interieure cuire noire avec facilites de paiement par leasing, Rs 1.300,000, Tel 915 0425

Proton Saga for sales YR OC 98.Rs 55000 a deb tel 9053152 for more queries feel free to contact.

Moteur Glanza - Jantes cosmiques Rs 230 000 neg. Tel : 472 91 88

FOR SALE ,UNIQUE OPPORTUNITY,ONE OWNER TOYOTA COROLLA MODEL AE100,model ee101, IN EXCELLENT CONDITION ,PRICE RS225000 A DEB, S-E LIMITED,FULLY...

Urgent a Vendre Nissan March annee 06 Full/option a/c p/s v/e CD Player 139,000 km Moteur 1240cc Manuel a Rs 340,000 a deb tel 7695536

Opel Astra a vendre pour cause de depart Voiture verte metallisee en tres bon etat, appareil music CD + Mp3 Pioneer, jantes cosmiques opel d'origine...

A vendre Nissan saloon B14 couleur blanche année 97 en bonne état, manuel, toutes options + Power steering + appareil CD/USB (jvc) +2 haut parleurs...

bmw e34 anne 93 excellent condition.192 hp.tel 7802431

Toyota vitz 2006 full options price reduced from Rs396000 to Rs380000. For sale now. Very good condition. Regularly serviced. 117000 Kms Four new...

AVENDRE TOYOTA NZE ANEE 02 F/OP 140000KM ENTRE BONE ETTA TEL 7039243

AVENDRE NISSAN AK12 ANEE 07 F/OP 135000KM LAIDY DRIVEN ETTA NEUF TEL 7039243

For sale Nissan March B14 elegance zn99 with full options,alloy wheels,white,tel 7519786

to be noted that the information is dynamic, that is it keeps changing

this sounds diffcult to pick right amount, because format is not fixed.

yeah thats the point. i cant find a way to do that /// HELP!

any how if we write some code, but you can not be sure, that it will pick right amount.
do you have any other website source with better html display elements?

I have to use a website where a user enters his information the way he wants, for example if he wants to sell his car,he can put it in the following format: rs 410000 / rs. 410000/ rs 410 000/ price rs 410 000 / rs 410k .. as u can see there are several ways of writing it,but i have to extract the value only that is 410000 without the space.
even if i take another website, the way i have to ecxtract the price will still be same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.