Search code examples
phpstrpos

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!


Solution

  • 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.