Adding a dollar sign in front of a number in a random text/numeral string

Posted on 2009-02-22
Last Modified: 2012-05-06
I am helping a client that wants to make sure that a dollar sign is in the appropriate place within a varchar field. I have 90% done however there is one instance that I didn't program for. Below are the a few possible scenarios that I have programed for, as well as the attached code.

only 50.00
only 50.00 for 4 of them

Now based on the attached code, I was successful in adding the dollar sign in front of the 50.00 so the output will look like this:

only $50.00
only $50.00 for 4 of them

The problem is that when the user types in:
4 for only 50.00
The attached code outputs
$4 for only 50.00

Basically I need to modify or rewrite the script to account for that issue. My current script grabs the first number in the string, then adds a dollar sign to it which is fine until a number that deals with quantity is added first. I realize that it would be easier to add a price field and a price description field, but I am not able to convince the client to go that route. Thanks for your help. Oh, the client doesn't mind if we add the dollar sign in front of the first instance of two numbers... if that helps. Thanks!
$price = "4 for only 50.00";;

        $price = str_replace("$","",$price);

        $just_numbers = ereg_replace("[^0-9]", "", $price);

        $first_number = $just_numbers[0];

        $new_number = "$]" . $first_number;

	$pattern = "/$first_number/";

	$replacement = "$new_number";

        $price = preg_replace($pattern, $replacement, $price, 1);

	$price = preg_replace('/$]/', '/$/', $price, 1);

	$price = str_replace("]","",$price);

        echo "$price";

Open in new window

Question by:Greg Alexander
    LVL 27

    Expert Comment

    And what would you want if the user typed in "$50 for only 4"?

    You'd be better to convince your client to do it right and use two separate fields.
    LVL 14

    Accepted Solution

    See below code, hope it works for you
    $price = "4 for only 50.00";
    $newprice = preg_replace('/(\d+?\.\d{1,})/im', '$$1', $price);
    echo $newprice;

    Open in new window

    LVL 107

    Assisted Solution

    by:Ray Paseur
    I agree with yodercm.  How about this? "get 4 of them for 50 until March 15, 2009, because they will be 65 for 3 after that!"

    You're hatching a catastrophe if you try to treat the quantity, text and price as a single field.  These are separate data elements and should be handled separately to avoid confusion.

    Then for any price field, you just do this:

    $formatted_price = '$' . number_format($price);

    Let common sense prevail!  Best, ~Ray
    LVL 14

    Expert Comment

    by:Ionut A. Tudor
    In my above code i treat float numbers 50.00 12.412 44.51 10.00 12.55 as prices. I did this because in all the OP examples i saw number 4 and float 50.00.
    Anyway while my script answer to his question, it is better to keep the price separated from number of items, like @yodercm and @Ray_Paseur agreed upon and with good reasons.
    Good luck
    LVL 107

    Expert Comment

    by:Ray Paseur
    @al3cs12: Good eye, and a good solution, even if the client insists on asking the wrong question!


    Best to all, ~Ray
    LVL 18

    Author Closing Comment

    by:Greg Alexander
    I guess the client will just have to deal with it.. Thanks ALL!

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now