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);
?>