Re: Picking a phrase out of a web page.

From: Date: Fri, 09 May 2003 23:40:52 +0000
Subject: Re: Picking a phrase out of a web page.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-147077@lists.php.net to get a copy of this message
I use regular expressions a lot to do this. You first want to match 'Price' in either upper or lower case, then any number of any characters to allow for a colon or spaces or 'each' or some other qualifier until you find a dollar sign, then one or more digits, and optionally a decimal point followed by two digits. Finally, you might want to capture the rest of the line to check for another price in the same line. This assumes you would have a leading zero for a sub-dollar price. I think the following will do this, and I will build the regular expression as I go: This is what the end expression will be: $pe = '/Price.*\$(\d+(\.\d\d)?)(.*)/i'; // PERL style regular expression to find price information Building logically: $pe = '/'; // opening delimiter for price expression $pe .= 'Price'; // match 'Price literally - later allow for case $pe .= '.*'; // match any number of any characters until a $ $pe .= '\$'; // escape a dollar sign to avoid special meaning $pe .= '('; // begin subpattern which will be captured $pe .= '\d+'; // match one or more digit $pe .= '('; // begin subpattern for optional decimal point and digits $pe .= '\.'; // escape decimal point to avoid special meaning $pe .= '\d\d'; // match two digits $pe .= ')?'; // close subpattern and indicate it is optional $pe .= ')'; // close pattern which is captured $pe .= '(.*)'; // capture the rest of the line $pe .= '/'; // tailing delimiter $pe .= 'i'; // trailing option indicating ignore case $yourarryelement = 'get yours now Price: each $4.56' ha-ha; // test string $price_found = preg_match($pe, $yourarrayelement, $match); // true if price found If a price is found, $match[1] will contain the price without the dollar sign, $match[2] will contain the cents if they exist, and $match[3] will contain any remaining characters after the match, "ha-ha" in this case, but in some cases there could be additional prices.

« previous php.general (#147077) next »