Re: String question
| From: | Jochem Maas | Date: | Wed, 15 Dec 2004 02:48:34 +0000 |
| Subject: | Re: String question | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-37689@lists.php.net to get a copy of this message | ||
Chris Payne wrote:
Hi there everyone, I am having to read a string with text in, but the way the DB is written in the same string you have the price, for example”this doesn't sound like a DB problem, sounds more like a regexp problem. (i.e. php-db is probably the wrong list)
CASARON 4G 50lb Sacks in Ton Lots $88.00 How can I strip out the 88.00 (Baring in mind it could be any number) into it’s own string?http://nl2.php.net/manual/en/ref.pcre.php NB: note the difference between using single and double quotes when defining your regexp. e.g: $regexp = '/^.*\$[ \t]?([0-9]+\.[0-9]{2})$/s'; and: $regexp = "/^.*\$[ \t]?([0-9]+\.[0-9]{2})$/s"; ...are not the same. assuming that the price is at the end of the string you and is preceeded by a dollar sign (with or without a seperating space) could do: <?php $myString = " CASARON 4G 50lb Sacks in Ton Lots $88.00 \r\n \t\r\n "; $matches = array(); $found = preg_match(
'/^.*\$[ \t]?([0-9]+\.[0-9]{2})$/s',
/* some commented out alternatives */
// '/^.*\$([0-9]+\.[0-9]{2})$/s',
// '/^.*\$([0-9]+\.00)$/s',
// '/^.*\$(88\.00)$/s',
// '/.*\$(\d+\.\d+)/s',
trim($myString), /* make life easier have a trim */
$matches /* passed by ref */
);
print_r("original string:\n--\n$myString");
print_r("\n\ntrimmed string:\n--\n".trim($myString)."\n\n");
print_r("found: $found\n matches array contents:\n\n");
print_r($matches);
?>
Thank you. Chris --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.816 / Virus Database: 554 - Release Date: 12/14/2004