/*
###########################################################################
This function separates a numerical value followed by a text label by in-
serting a single space between them, if there is none already, in order to
facilitate subsequent parsing of the separate string elements.
Examples:
The argument ('12.345km')
would return '12.345 km'
The argument ('1.2345e-23m')
would return '1.2345e-23 m'
etc.
Any subsequent spaces within the text or label part remain unchanged.
Example: The argument ('3blackbirds in a cage')
would return '3 blackbirds in a cage'
with only 1 space inserted after the initial number (3) and all
subsequent spaces considered part of the text label or symbol
part of the string and preserved.
If the argument is a purely numerical value, then it is returned as-is.
If the argument is only a non-numerical string, then it is returned as-is.
The returned string will include a status code: OK | NaN
delimited by a vertical bar | character. (This delimiter can be changed).
'OK' means that the numeric part tests as valid.
'NaN' means that the numeric part cannot be resolved as a valid number.
NO DEPENDENCIES
###########################################################################
*/
function Split_Num_and_Text ($NumAndTextStr)
{
$X = trim($NumAndTextStr);
// ------------------------------------------------
// If purely numeric, then return as-is, unchanged.
if (Is_Numeric($X)) {return $X;}
$L = StrLen($X);
$NumericPart = $AlphaPart = '';
for($i=0; $i < $L; $i++)
{
$x = substr($X,$i,1);
if (
Is_Numeric($x)
or $x == '.'
or $x == '-'
or $x == '+'
or StrToUpper($x) == 'E'
)
{$NumericPart .= $x;}
else
{$AlphaPart = trim(substr($X,$i,$L)); break;}
}
$StatusPart = ((Is_Numeric($NumericPart))? 'OK' : 'NaN' );
return trim("$NumericPart $AlphaPart| $StatusPart");
} // End of Split_Num_and_Text (...)