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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -