これは、価格を抽出する必要があるデータの文字列のサンプルです。
「価格 Rs 475 000 - CHEVROLET AVEO LS // SEP 11 6,000 kms - 赤.. フル オプション.. マニュアル 5 ドア // ハッチバック 786 8394 で私に連絡してください」
特定の Web サイトをクロールした後、そのような文字列が多数ありますが、文字列には任意の数または単語が含まれている可能性があります。
各単語をスペースで区切り、配列 $arr に格納しようとしました。価格 $arrPrice の識別子を格納する別の配列を宣言しました。price または rs という単語が見つかった場合、データ (たとえば 475 000) が変数 $price に格納されます。ただし、スペースで分解したため、 000 は考慮されていません。xml タグで 475 しか取得できません。
それを行う効率的な方法は正規表現を使用することかもしれませんが、私はそれが苦手です。誰かが私を助けることができれば感謝します。
今まで私のコードの下を見つけてください、
ありがとう!
<?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);
?>