php from a string get or print specialchar, number, alphabetic seprate seprate in php -
i have string like:
$str = "hello@$how%&*!are345you^_there56";
i want alphabet stores in 1 variable like:
hello,how,are,you,there
number should stored in 1 variable like:
3,4,5,5,6
special characters separately:
@$%&*!^_
how can this?
in opinion, best choice use preg_split
:
<?php $str = 'hello@$how%&*!are345you^_there56'; $words = array_filter(preg_split('/[^a-za-z]/', $str)); $numbers = str_split(join('', preg_split('/[^0-9]/', $str))); $specials = str_split(join('', preg_split('/[a-za-z0-9]/', $str))) print_r($words); print_r($numbers); print_r($specials);
by negating character classes can filter results how want. str_split
join
calls split on character basis rather on group basis.
results:
array ( [0] => hello [2] => how [6] => [9] => [11] => there ) array ( [0] => 3 [1] => 4 [2] => 5 [3] => 5 [4] => 6 ) array ( [0] => @ [1] => $ [2] => % [3] => & [4] => * [5] => ! [6] => ^ [7] => _ )
Comments
Post a Comment