Jump to content


Photo

String to Value


  • Please log in to reply
5 replies to this topic

#1 Erazer

Erazer
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 21 August 2006 - 08:26 PM

Hi
I'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 question
thanks

#2 Jeremysr

Jeremysr
  • Members
  • PipPipPip
  • Advanced Member
  • 199 posts
  • LocationSaskatchewan, Canada

Posted 21 August 2006 - 08:48 PM

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.

$string = "45.9";
$int_string = intval($string);
$float_string = floatval($string);
echo ($int_string); // Will echo 45
echo ($float_string); // Will echo 45.9


#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 21 August 2006 - 09:24 PM

Sloppy but functional:
<?php
$string = "28/10";
$bits = explode("/",$string); // split string at /
if (count($bits)>1) {
    $string = $bits[0]/$bits[1];
}
echo $string; // output 2.8
?>

Legend has it that reading the manual never killed anyone.
My site

#4 Erazer

Erazer
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 21 August 2006 - 09:30 PM

Hi Jeremysr
thanks 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.9

but

$string = "45.9+10";
$float_string = floatval($string);
echo ($float_string); // Will also echo 45.9 and not 55.9

so i'm looking for something that will do the math in the string and out the result

thanks



#5 Erazer

Erazer
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 21 August 2006 - 09:38 PM

Hi AndyB
that 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

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 21 August 2006 - 09:53 PM

or

<?php
$str = "28/10";
eval ("\$x=$str;");
echo $x;
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users