Catch a variable string after a prefined needle with strpos()
I have a tricky problem to solve. I already got a push in the right direction by this question and answers: How to Find Next String After the Needle Using Strpos()
But my problem is a bit more tricky.
This is the context:
Users enter an amount in a comment field by entering a predefined term ('amount'), followed by the number. This is a free field, so I need to take into account the various ways in which users enter the amount. With or without currency indicator, decimals, etc.
开发者_高级运维Example:
$commentfield = "2011-7-5 Send bid. Amount: € 3482,- Also included Lease option";
or, another example:
$commentfield = "2011-6-5 Bid accepted. Amount: 235. Client requested quick delivery";
Now, I need to extract that amount to be able to sum it. Is there anyone who can push me in the right direction?
Thanks!
Sounds like a job for regular expressions using PHP function preg_match. You will need to get a list of all possible formats people already use for the amounts.
Going forward, you might want to make amount a separate field all together.
I think this may be useful.
$s ="2011-7-5 Send bid. Amount: € 3482,- Also included Lease option";
preg_match('/amount(\s*):(\s*)€(\s*)(?P<price>\d+)/i',$s,$match);
echo $match['price'];
精彩评论