I have a variable
$string = "(123) 011 - 34343678";
and I want 12301134343678 as an output in integer data type. How can I do this without using any predefined function in PHP or in any other programming language.
Well it's not the nicest solution, but something like this could work for you:
Here I simply loop through all characters and check if they are still the same when you cast them to an integer and then back to a string. If yes it is a number otherwise not.
<?php
function own_strlen($str) {
$count = 0;
while(@$str[$count] != "")
$count++;
return $count;
}
function removeNonNumericalCharacters($str) {
$result = "";
for($count = 0; $count < own_strlen($str); $count++) {
$character = $str[$count];
if((string)(int)$str[$count] === $character)
$result .= $str[$count];
}
return $result;
}
$string = "(123) 011 - 34343678";
echo removeNonNumericalCharacters($string);
?>
output:
12301134343678
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With