Calling all PHP Gurus! Complex problem inside!
Written By ldragon on Apr. 16, 2008.
9 Comments
Report Note
+ Clip This
There, that got you attention.
I've got a (annoying) client that I'm making a Caribbean property website. She has the functionality to manage the $ to £ and $ to Euro conversion rates, and she only enters the prices in $ in the admin. For the normal pricing section this isn't a problem, since there are individual fields, so conversion is easy.
The problem is the special offers part for each property, since this has to be text so she can be whatever she wants in these fields. But she is insisting on entering the price in $ but wants them to appear in £ on the front-end.
See why she's annoying? I've been looking at the PHP functions such as strpos, strstr, str_replace trying to find a way to automate this.
Basically I'm trying to develop a function that takes a string input and converts all $ prices into £ Sterling. Any ideas would be appreciated, since you're such a helpful lot :)
Here's an example of the type of text in there (I'm moving it all from an older ASP .NET site):
Special Offer:
SPRING SALE
Book before 30 April for travel between 01 March - 31 July 2008.
01-30 April
Standard room £93.00
Deluxe Pool/Gdn View £102.00
Superior Dlxe Pool/Gdn £110.00
1 -31 May
Standard room £85.00
Deluxe Pool/Gdn View £93.00
Superior Dlxe Pool/Gdn £102.00
1 June-31 July
Standard room £80.00
Deluxe Pool/Gdn View £87.00
Superior Dlxe Pool/Gdn £97.00
All prices are per person per night.

Gnorb
Written Apr. 16, 2008 / Report /
With that headline I was fully expecting a rickroll.
/Not a PHP person.
//Are you ever gonna give me up?
Ozone42
Written Apr. 16, 2008 / Report /
You might be overcomplicating things.
The field in question, you say it has to be text so she can enter whatever she wants. Is she entering text here? Doesn't really make sense for a price or discount field, it should just be a decimal. You can prepend the currency symbol on output.
The only real complexity comes in determining the exchange rate. It changes all the time, so if you need to be accurate, then you need to get the exchange rate for the day. I'm sure there's providers of this information you can hook into. Google has it for instance. If you want to give real time accurate conversions you'll have to pull in and parse this from an external source.
How accurate do you need to be? If your client is content with inputting the exchange rate say once a week, you could just make a configuration option she has access to. This is very prone to user error/forgetfulness,
Either way you decide to go, once you have the rate your task is just one of multiplication.
Scrivs
Written Apr. 16, 2008 / Report /
I agree with Ozone there is nothing complicated about this at all. Without even going into code all you are doing is:
Dollars * Exchange Rate = Amount you want
If you can't find a place to hook into exchange rates just setup a constant variable in a file and set the exchange rate there.
ldragon
Written Apr. 16, 2008 / Report /
Don't think I've explained myself very well, the field in question will contain text, be it lists, paragraphs, anything, it's not feesible to say 'special offers prices here' since there will be text accompanying it, and there may not be any prices. It's just if there is, they need to be converted on the fly. Some properties do not have special offers, then this field left blank.
The function needs to go into the text and find any occurences of a price in $ and convert it to £. The rate is defined elsewhere, thats done and dusted.
Scrivs
Written Apr. 16, 2008 / Report /
Wait, wah? Why are the allowed to enter any text and then put a random number in there only to be converted later? I would change that right up because that is silly. If it can't be done it looks like you might have to do some regex and conversions, have fun.
Ozone42
Written Apr. 16, 2008 / Report /
Break it into two fields, one a text descriptor, the other the price. If there is the possibility for more than one special price, a new table is in order specifically for those and their descriptors.
ldragon
Written Apr. 16, 2008 / Report /
The responses I feared :( Ah well, thanks for the input, might try using Regex, event if I can get the values out it will be a GREAT SUCCESS!
Think I might just say to her 'Do it yourself love!'. At least I wasn't alone in thinking the whole idea is bonkers.
Oli
Written Apr. 16, 2008 / Report /
I don't see regex being too difficult here. You want to match something like: \$([\d]+(?:\.[\d]{2}))
That should match: $123123 and $123.23
Get that as a list of matched values, then just string replace the captured values (match group 0) with the converted rate. Don't forget you need to convert from string to float before you start getting hardcore with the numbers.
Ideally you'd cache the output to temporary file too as all this jazz is expensive CPU wise.
ldragon
Written Apr. 16, 2008 / Report /
Wow thanks for that stuff Oli, never delved into regex much so far, that's helped me a lot. To be fair to this client she is our biggest job in years, so we're pulling out all the stops.