Deviding a variable into numbers and the others

I like to devide a variable into numbers and the others.

The would-be code below doesn’t work correctly but I hope it show what I want.

[b]would-be code[/b]
$myVar ='ab[COLOR="#ff0000"]4[/COLOR]ged[COLOR="#ff0000"]23[/COLOR]a[COLOR="#ff0000"]9[/COLOR]cz[COLOR="#ff0000"]8493[/COLOR]k';

[COLOR="#FF0000"]$numberOnly[/COLOR] = numberOnly($myVar);
$non-numberOnly=on-numberOnly($myVar);

echo $numberOnly ;
echo $non-numberOnly ;

[b]result[/b]
[COLOR="#FF0000"]42398493[/COLOR]

abgedaczk

$onlyNumbers = implode('', array_filter(
  explode('', $myVar),
  function($a) { return ctype_digit($a); }
));
$onlyOthers = implode('', array_filter(
  explode('', $myVar),
  function($a) { return !ctype_digit($a); }
));

PHP>=5.3+ only
For PHP<=5.2 use [fphp]create_function[/fphp]

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.


$myVar = 'a1b2c3d4e5f6g7h8i9j0';
$onlyNumbers = implode('', array_filter(str_split($myVar), function($a) { return ctype_digit($a); }));
$onlyOthers = implode('', array_filter(str_split($myVar), function($a) { return !ctype_digit($a); })); 

1 Like
[b]code1(ScallioXTX) [/b]
$onlyNumbers = implode('', array_filter(
  explode('', $myVar),
  function($a) { return ctype_digit($a); }
));

[b]code2 (modernW)[/b]
$onlyNumbers = implode('', array_filter(str_split($myVar), function($a) { return ctype_digit($a); })); 

As I test both code1 and code2, they produce syntax error, unexpected T_FUNCTION.

I can’t find what is the cause of the error, Pairing of parenthesis or bracket seems correct.

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

Thanks

Try this:


##########################################
function just_numbers($str=NULL, $numbers=TRUE)
{
  $nums = '';
  $chrs = '';

  for( $i2=0; $i2<strlen($str); $i2++ ):

    $value=$str[$i2];

    if( is_numeric($value) )
    {
      $nums .= $value;
    }
    else
    {
      $chrs .= $value;
    }
  endfor;

  return $numbers ? $nums : $chrs;
}

$myVar ='ab4ged23a9cz8493k';

echo '<br />nill == ', just_numbers();
echo '<br />nums == ', just_numbers($myVar);
echo '<br />chrs == ', just_numbers($myVar, FALSE);


#output

nill ==
nums == 42398493
chrs == abgedaczk
$myVar == ab4ged23a9cz8493k

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 :rolleyes: