0

I have such array:

$array = array('abC12', 'bC44', 'Am286c$', 'cC092', 'cC09288');

With using rexexp it is necessary to, at first, delete symbols (replace by ''), that are not in [A-Ca-c0-9]. At second, it is necessary to delete from array variables that not match such condition: string length not equal to 5 (values 'bC44' and 'cC09288').

So, as result array must contain:

$array = array('abC12', 'A286c', 'cC092');

Thanks you for any help!

Vladimir.

user1978142
  • 7,946
  • 3
  • 17
  • 20
  • I tried, for example this: $array = preg_replace('/^[a-c0-9]{5}$/i', '', $array); – VladimirSap May 29 '14 at 07:51
  • ok,you can take help from this for replacing symbols and spaces in the string http://stackoverflow.com/questions/14114411/remove-all-special-characters-from-a-string – Vandana Pareek May 29 '14 at 07:53

2 Answers2

1
$result = array();
foreach ($array as $val) {
    $val = preg_replace('/[^a-c0-9]/i', '', $val); // Remove symbols
    if (strlen($val) == 5) { // Check string length
        $result[] = $val;
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Barmar, thanks you very much! Is it possible to use only preg_replace (somehow with {32})? So not to use if block – VladimirSap May 29 '14 at 07:48
  • `preg_replace` just returns the updated string, it won't have any effect on whether the string is put in `$result`. – Barmar May 29 '14 at 07:54
0

How about:

$array = array('abC12', 'bC44', 'Am286c$', 'cC092', 'cC09288');
$array = array_filter(preg_replace('/[^a-c0-9]/i', '',$array),function ($var) {return strlen($var) == 5;});
print_r($array);

output:

Array
(
    [0] => abC12
    [2] => A286c
    [3] => cC092
)

preg_replace acts also on array.
Documentation preg_replace, array_filter

Toto
  • 89,455
  • 62
  • 89
  • 125