0

I have this string (comes from the GoDaddy listing xml) and I wish to assign each of the values to a variable:

Auction Type: Offer, Auction End Time: 08/12/2012 05:57 AM (PDT), Price: $4,000, Number of Bids: 0, Domain Age: 0, Description: 1234 I declare a thumb war, 1234ideclareathumbwar.com, Traffic: 0, Valuation: $0, IsAdult: true

My question is, how do I do so using regexp?

I've tried this out:

$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])
    );
}

but it does not work. May I ask for assistance?

Thank you!

1
  • Have you printed result array after match ? Commented Aug 13, 2012 at 6:56

2 Answers 2

1

As Leon Kramer said, you have to change the last element of your re to:

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

and, also, the line $re .= "Traffic: (\d*)(.*?), "; holds 2 groups (i.e. $m[7] and $m[8]), so change

$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])
);

to

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

I don't know what you want to do with $m[8] that is empty in the given example.

Sign up to request clarification or add additional context in comments.

Comments

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: (.+?)$";

you forgot one , also I changed , to $ for the last part of $re

1 Comment

Whoa! There it is! Thanks a lot! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.