I would love to know, if it's possible to get a specific data from a specific mysql table..

Let say on the table itself has a Varchar ... mixed of words and floats..

and then ... I want to pull only the float from the table using php...

can it be done ??

Recommended Answers

All 8 Replies

Can you give an example of your table and the data that is in it, and an example of the output you require ?

if you are looking to pull a float out of a var char field that has for example text wrapped around it (i.e. "Temp: 26.2 Degrees") then I would suggest using preg_match or something alike to do a regular expression to extract it. I'm sure there is other ways too.

yeah ... something like that ... it's a long var char...

something like... "hello, my name is jack.. my height is 5.5 feet"

let say .. I just want to pull only "5.5"

$fullstring = "hello, my name is jack.. my height is 5.5 feet";
preg_match_all('/[0-9].[0-9]/', $fullstring, $matches);
$count = count($matches[0]);
# this will tell you how many times "x.x" was found in string
echo $count; 

# this will print out each "x.x" it found
for ($i = 0 ; $i < $count ; $i++ ) 
{
echo $matches[0][$i];
echo '<br/>';
}

Use pattern matching for it....Use can see how to do this from the examples in this link

<?php
preg_match('/^\d+$/', $price[1]);
?>

You can also try

$var = '122.34343The';
$float_value_of_var = floatval($var);
echo $float_value_of_var; // 122.34343

The following regex would also match 45.67 and -45

/-?[0-9]+(\.[0-9]+)?/

tq guys... I'll give a try later..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.