Erazer Posted August 21, 2006 Share Posted August 21, 2006 HiI'm using the EXIF functions to read camera setting of my photos. In some cases it returns the value as a string e.g 2.8 is returned as "28/10". Is there a function to convert the string into a numeric value?sorry if this seems newbie questionthanks Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted August 21, 2006 Share Posted August 21, 2006 intval() will convert a variable to an integer, I think that's what you want.$string = intval($string);Edit: Also, floatval() will return a floating-point value.[code=php:0]$string = "45.9";$int_string = intval($string);$float_string = floatval($string);echo ($int_string); // Will echo 45echo ($float_string); // Will echo 45.9[/code] Quote Link to comment Share on other sites More sharing options...
AndyB Posted August 21, 2006 Share Posted August 21, 2006 Sloppy but functional:[code]<?php$string = "28/10";$bits = explode("/",$string); // split string at /if (count($bits)>1) { $string = $bits[0]/$bits[1];}echo $string; // output 2.8?>[/code] Quote Link to comment Share on other sites More sharing options...
Erazer Posted August 21, 2006 Author Share Posted August 21, 2006 Hi Jeremysrthanks for the quick reply, I've tried the floatval() before but it doesnt seem to work if there is a operand in the string so$string = "45.9";$float_string = floatval($string);echo ($float_string); // Will echo 45.9but$string = "45.9+10";$float_string = floatval($string);echo ($float_string); // Will also echo 45.9 and not 55.9so i'm looking for something that will do the math in the string and out the resultthanks Quote Link to comment Share on other sites More sharing options...
Erazer Posted August 21, 2006 Author Share Posted August 21, 2006 Hi AndyBthat works great so thanks for the help. I had a feeling it needed some string manipulation and break down but since i've just started learning your sloppy code is an eye opener ;)thanks again Quote Link to comment Share on other sites More sharing options...
Barand Posted August 21, 2006 Share Posted August 21, 2006 or[code]<?php$str = "28/10";eval ("\$x=$str;");echo $x;?>[/code] 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.