New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
PHP question
in Help
HI all,
Just a quick one, what's the best way to remove the part before a decimal place and the part after a decimal place in a string.
E.g
string of "12345.567843
I need to return both 12345 and 567843 seperately
Thanks in advance.

Comments
use the explode function
$source = "12345.567843"; $result = explode(".", $source); echo $result[0]; // 12345 echo $result[1]; // 567843Outputs:
Perfect, thanks!
GOD BLESS OVERKILLING
preg_match_all('|([0-9]*)\.([0-9]*)|', $in, $out); print_r($out);But regex's are so slow
$value = 12345.567843; $first_part = floor($value); $second_part = str_replace("${first_part}.", '', $value);In reality I'd probably do:
list($first_part,$second_part) = explode('.', $value);After first testing that $value is in the expected format.
I feel like we should have a competition to see who can do this using the most code.
What did I start...
It's interesting to see how many different ways a simple task can be accomplished
I can take a nap while that preg_match is running ;-)
What no sed or awk? This thread would not be complete without it.
$firstpart = ''; $lastpart = ''; $array = array(); $string = '12345.567843'; $stringsize = strlen($string); for($i=0; $i<$stringsize; $i++) { $array[] = $string[$i]; if($string[$i] == '.') { $dotposition = $i; } } foreach($array as $key => $val) { if($key < $dotposition) { $firstpart .= $val; } elseif($key > $dotposition) { $lastpart .= $val; } } echo $firstpart; echo "\n"; echo $lastpart;lol you guys seems bored...
$string = '12345.567843'; $array = str_split($string); $dotposition = array_search('.', $array); $firstpart = implode('', array_slice($array, 0, $dotposition-1)); $lastpart = implode('', array_slice($array, $dotposition+1, count($array)-1)); echo $firstpart; echo "\n"; echo $lastpart;This only removes the decimal. Too lazy to figure out the rest.
echo '12345.567843' | sed 's/.//g'$firstpart = ''; $string = '12345.567843'; $i = 0; while(true) { $firstpart .= $string[$i]; if($string[$i] == '.') { break; } $i++; } $lastpart = str_replace($firstpart, '', $string); echo rtrim($firstpart, '.'); echo "\n"; echo $lastpart;That removes everything.
I guess that you mean
echo '12345.567843' | sed 's/\.//g'Yeah, silly forum engine.
pretty bored.
Thanks for catching that. Yea, posted without the {code} tags at first and it must have stripped that.
print substr($word, 0, strpos($word,".")). " ". substr($word, strpos($word,".")+1);