You can’t use the explode() function with an empty delimiter. You’ll have to use the str_split() function to make each character in the string a value in an array.
In around 5 minutes i was able to come up with this
<?php
$mixed='ab4ged23a9cz8493k';
//input string
//returns only numeric values from the string
//type 1 to get numbers 2 to get alphabet
function onlyNumbersOrText($string1,$type){
//converting to array to run foreach,foreach does not work in string like in .net
$splitString=str_split($string1);
$appendValue='';
$functionToUse=($type==1)?'is_numeric':'ctype_alpha';
foreach ($splitString as $key=>$value){
if($functionToUse($value)){
$appendValue .=$value;
}
}
return $appendValue;
}
echo onlyNumbersOrText($mixed,1);
echo onlyNumbersOrText($mixed,2);
?>
i agree there can be lots of improvements to this function but still does to job for now
If you want to non numbers part(not only text) then have a else part rather than using ctype function and return the else part
Probably because you’re using PHP <= 5.2, I specifically said it only works on PHP>=5.3 and if you’re using an older version you should use [fphp]create_function[/fphp] instead.
@tpunt; ah yes, str_split. If I got a dollar for every time I made that mistake