0

我有这个字符串(来自 GoDaddy 列表 xml),我希望将每个值分配给一个变量:

拍卖类型:报价,拍卖结束时间:08/12/2012 05:57 AM (PDT),价格:4,000 美元,投标数量:0,域名年龄:0,描述:1234 我宣布拇指战争,1234ideclareathumbwar.com,流量:0,估值:0 美元,IsAdult:真

我的问题是,我该如何使用正则表达式?

我已经试过了:

$re  = "Auction Type: (.+?), ";
$re .= "Auction End Time: (.+?) \(.+?\), ";
$re .= "Price: .(.+?), ";
$re .= "Number of Bids: (.+?), ";
$re .= "Domain Age: (.+?), ";
$re .= "Description: (.*?), ";
$re .= "Traffic: (\d*)(.*?)";
$re .= "Valuation: (.+?), ";
$re .= "IsAdult: (.+?), ";

if(preg_match("~".$re."~is",$description,$m)){
    $record = Array('auctiontype' => trim($m[1]),
        'endtime'     => strtotime($m[2]),
        'price'       => str2float($m[3]),
        'bids'        => trim($m[4]),
        'age'         => trim($m[5]),
        'description' => addslashes(trim($m[6])),
        'traffic'     => trim($m[7]),
        'valuation'   => trim($m[8]),
        'isadult'     => trim($m[9])
    );
}

但它不起作用。我可以寻求帮助吗?

谢谢!

4

2 回答 2

1

正如 Leon Kramer 所说,您必须将 re 的最后一个元素更改为:

$re .= "IsAdult: (.+)$";

而且,该行$re .= "Traffic: (\d*)(.*?), ";还包含 2 个组(即 $m[7] 和 $m[8]),所以改变

$record = Array('auctiontype' => trim($m[1]),
    'endtime'     => strtotime($m[2]),
    'price'       => str2float($m[3]),
    'bids'        => trim($m[4]),
    'age'         => trim($m[5]),
    'description' => addslashes(trim($m[6])),
    'traffic'     => trim($m[7]),
    'valuation'   => trim($m[8]),
    'isadult'     => trim($m[9])
);

$record = Array('auctiontype' => trim($m[1]),
    'endtime'     => strtotime($m[2]),
    'price'       => str2float($m[3]),
    'bids'        => trim($m[4]),
    'age'         => trim($m[5]),
    'description' => addslashes(trim($m[6])),
    'traffic'     => trim($m[7]),
    'valuation'   => trim($m[9]),   // here
    'isadult'     => trim($m[10])   // and here
);

在给定的示例中,我不知道你想做什么$m[8]是空的。

于 2012-08-13T08:29:56.557 回答
1
$re  = "Auction Type: (.+?), ";
$re .= "Auction End Time: (.+?) \(.+?\), ";
$re .= "Price: .(.+?), ";
$re .= "Number of Bids: (.+?), ";
$re .= "Domain Age: (.+?), ";
$re .= "Description: (.*?), ";
$re .= "Traffic: (\d*)(.*?), ";
$re .= "Valuation: (.+?), ";
$re .= "IsAdult: (.+?)$";

你忘了一个,我也改成,$$re 的最后一部分

于 2012-08-13T07:41:15.827 回答