Jump to content

Archived

This topic is now archived and is closed to further replies.

Erazer

String to Value

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 45
echo ($float_string); // Will echo 45.9
[/code]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
or

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.