3

looking for the code can remove characters from the array and display numbers only.

array( 
    1=>123456 hello; / &, 
    2=>128767 ^% * ! ajsdb, 
    3=>765678 </ hello echo., 
); 

i want to remove the floowing from the array

hello; / &
^% * ! ajsdb
</ hello echo.

and wants to keep as stated

array( 
    1=>123456, 
    2=>128767, 
    3=>765678, 
); 

Thanks and Kind Regards,

3
  • you will need a regular expression for that
    – Faizan Ali
    Jul 12, 2012 at 13:59
  • 8
    Would this happen to be homework?
    – 0b10011
    Jul 12, 2012 at 13:59
  • Are those even valid array elements?
    – Aditya M P
    Jul 12, 2012 at 14:00

8 Answers 8

14

You want to use preg_replace to replace all non-numeric chars with ''

$arr = array(
    1 => "1234 perr & *",
    2 => "3456 hsdsd 3434"
);

foreach($arr as &$item) {
    $item = preg_replace('/\D/', '', $item);
}

var_dump($arr);

results in

array(2) { [1]=> string(4) "1234" [2]=> &string(8) "34563434" } 
0
2

Make a for statement to get values of your array and try this:

    foreach($arr as $value){
        $cleansedstring = remove_non_numeric($value);
        echo $cleansedstring;
    }


function remove_non_numeric($string) {
return preg_replace('/\D/', '', $string)
}
2
<?php

// Set array
$array = array( 
    1 => "123456 hello; / &", 
    2 => "128767 ^% * ! ajsdb", 
    3 => "765678 </ hello echo.",
);

// Loop through $array
foreach($array as $key => $item){
    // Set $array[$key] to value of $item with non-numeric values removed
    // (Setting $item will not change $array, so $array[$key] is set instead)
    $array[$key] = preg_replace('/[^0-9]+/', '', $item);
}

// Check results
print_r($array);
?>
1
function number_only($str){
    $slength = strlen($str);
    $returnVal = null;
    for($i=0;$i<$slength;$i++){
        if(is_numeric($str[$i])){
            $returnVal .=$str[$i];
        }
    }
    return $returnVal;
}
0

You should use preg_replace using [0-9]+

0

like this

$values = array(
    1=>"123456 hello; / &",
    2=>"128767 ^% * ! ajsdb",
    3=>"765678 </ hello echo",
);

$number_values = array();
foreach($values as $value) {
    $pieces = explode(' ', $value);
    $numbers = array_filter($pieces, function($value) {
        return is_numeric($value);
    });

    if(count($numbers) > 0)
    {
        $number_values[] = current($numbers);
    }
}

print_r($number_values);
0

I would advice you to take a look at the intval method (http://php.net/manual/en/function.intval.php) and the foreach loop (http://php.net/manual/en/control-structures.foreach.php).

With those 2 functions combined you will be able to clear all the elements from the not numeric characters,

0

Why not array_walk() ? http://php.net/manual/en/function.array-walk.php

$arr = array(
    1 => "1234 perr & *",
    2 => "3456 hsdsd 3434"
);

array_walk($arr, function(&$item) { 
    $item = preg_replace('/\D/', '', $item);
});

print_r($arr);

Result:

Array
(
    [1] => 1234
    [2] => 34563434
)

Check it online: http://sandbox.onlinephpfunctions.com/code/d63d07e58f9ed6984f96eb0075955c7b36509f81

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.