Link to home
Start Free TrialLog in
Avatar of Greg Alexander
Greg AlexanderFlag for United States of America

asked on

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

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.

50.00
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:

$50.00
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

Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of Ionut A. Tudor
Ionut A. Tudor
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
@al3cs12: Good eye, and a good solution, even if the client insists on asking the wrong question!

;-)

Best to all, ~Ray
Avatar of Greg Alexander

ASKER

I guess the client will just have to deal with it.. Thanks ALL!