Php 从html字符串提取价格并转换为xml

Php 从html字符串提取价格并转换为xml,php,html,xml,Php,Html,Xml,这是我需要提取价格的数据字符串示例 “价格Rs 475000-雪佛兰乐骋LS//2011年9月6000公里-红色..全套选项..手动5门//掀背车-请致电786 8394与我联系” 我有很多这样的字符串后,爬网一个特定的网站,可以有任何数字或字的字符串 我试图用空格分隔每个单词,并将其存储在数组$arr中。我声明了另一个数组,用于存储price$arrPrice的标识符。 如果找到单词price或rs,则数据(例如475000)存储在变量$price中。然而,由于我已经用空间对其进行了分解,它没

这是我需要提取价格的数据字符串示例

“价格Rs 475000-雪佛兰乐骋LS//2011年9月6000公里-红色..全套选项..手动5门//掀背车-请致电786 8394与我联系”

我有很多这样的字符串后,爬网一个特定的网站,可以有任何数字或字的字符串

我试图用空格分隔每个单词,并将其存储在数组$arr中。我声明了另一个数组,用于存储price$arrPrice的标识符。 如果找到单词price或rs,则数据(例如475000)存储在变量$price中。然而,由于我已经用空间对其进行了分解,它没有考虑到000。我只得到了475个xml标记

有效的方法可能是使用正则表达式,但我并不擅长。如果有人能帮助我,我将不胜感激

在下面找到我的代码直到现在

谢谢

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


依此类推。

价格是否总是处于相同的位置?不,这不是标准,它可以处于任何位置。Thanx Salman。正如我提到的,我有几个已爬网的字符串,但我没有在其各自的xml标记中获得每个价格。也许您必须编写不同的逻辑来解析不同的字符串。我曾考虑使用preg_match_all(),但正如我所说,我不擅长正则表达式:(
if ( $arr[$i] == $arrPrice[$j] ) {
  $price = $arr[$i+1];
  if ( isset( $arr[$i+2] ) && is_numeric( $arr[$i+2] ) ) {
    $price .= $arr[$i+2];
  }
}