Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2009-02-22
Medium Priority
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

by:Cornelia Yoder
ID: 23706124
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

Ionut A. Tudor earned 1140 total points
ID: 23706144
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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 80 total points
ID: 23706503
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 14

Expert Comment

by:Ionut A. Tudor
ID: 23706579
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 111

Expert Comment

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


Best to all, ~Ray
LVL 19

Author Closing Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 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