How to move all special characters to the end of a string in PHP

1 Answer

0 votes
function move_special_characters_to_end($s) { 
    $len = strlen($s); 
    $chars = "";
    $pecial_characters = ""; 
          
    for ($i = 0; $i < $len; $i++) { 
         $ch = $s[$i]; 
         if (ctype_alnum($ch)) {
            $chars = $chars . $ch; 
         }
         else {
            $pecial_characters = $pecial_characters . $ch; 
         }
    } 
    return $chars . $pecial_characters; 
} 
  

$s = "c++14@vb.net&%java*() php <>/python 3.7.3"; 

echo move_special_characters_to_end($s);
     

 
   
/*
run:
        
c14vbnetjavaphppython373++@.&%*() <>/ ..
 
*/

 



answered Aug 15, 2019 by avibootz

Related questions

1 answer 363 views
2 answers 353 views
2 answers 344 views
1 answer 268 views
2 answers 303 views
1 answer 275 views
...