Scooby08 Posted January 21, 2010 Share Posted January 21, 2010 I was just wondering if php can convert a fraction that I'm getting from an xml feed.. The fractions are like so: 4½ Otherwise I was thinking of doing a str_replace like so: str_replace('½','.5',$fraction); But that gave me another problem.. It comes out like so: 4�.5 Any suggestions how I could just get 4.5? Thanks!! Quote Link to comment Share on other sites More sharing options...
teamatomic Posted January 21, 2010 Share Posted January 21, 2010 to convert a fraction simply divide the numerator by the denominator n/d: 1/2 would be 1 divided by 2. format it to 2 decimal places. Look at the source and see how the fraction is generated. If its ascii only 1/4,1/2 and 3/4 are available and you would just have to convert the symbol. If there are other fractions then probable just explode it on the slash and divide. if its a graphic and does not have alt description then YOYO. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted January 21, 2010 Author Share Posted January 21, 2010 I would but I don't have control over how the xml is feeding me the fraction.. Am receiving them like so: 4½ Not like so: 4 1/2 Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted January 21, 2010 Share Posted January 21, 2010 It sounds to me like you will have to match the character code to the appropriate mathematical operation. Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted January 21, 2010 Author Share Posted January 21, 2010 So how's that done?? preg_match are you meaning?? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted January 21, 2010 Share Posted January 21, 2010 Not talking about having control of the output, just what the raw code is. <fraction>4 3/4</fraction> <fraction 4 ¾</fraction> <fraction>4 ¾</fraction> will all display 4 3/4 but are not the same and wont be handled the same, code wise, when you have to work on converting them. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted January 21, 2010 Author Share Posted January 21, 2010 Well I don't have control over that as well.. I didn't make the xml file.. Can't touch it whatsoever.. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted January 21, 2010 Share Posted January 21, 2010 Ah...but obviously you can access it. Look a the raw file, the source. If not how can you expect to work on the file??? Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted January 21, 2010 Author Share Posted January 21, 2010 Yes I can access it.. I guess I'm missing your point.. Could you explain more? How do I convert the symbol?? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted January 21, 2010 Share Posted January 21, 2010 look at the file source and determine what is being used to create the fraction. refer to my earlier post as to the 3 ways to do a fraction in html. HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted January 21, 2010 Author Share Posted January 21, 2010 Its a symbol.. How do you suggest I convert? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted January 21, 2010 Share Posted January 21, 2010 Then just preg_match and convert, easy ¾ ¾ The above examples are 3/4, the only ones available are 1/4,/1/2 and 3/4. Look at an ascii table http://www.ascii.cl/htmlcodes.htm HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
Scooby08 Posted January 21, 2010 Author Share Posted January 21, 2010 I did that and then I ran into this problem: 4�.5 I did happen to get this one to finally work though.. preg_replace('/[^A-Za-z0-9]+$/','.5',$string); That one removed all characters that weren't letters or numbers, including that hidden question mark character.. Then it just replaced the ½ at the end.. In my case I will only have ½ if there are fractions, so I can just replace all those with .5.. Thanks for helping out on this one teamatomic!! I really appreciate it.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.