How to reverse a string without affecting the special characters in PHP

1 Answer

0 votes
function reverse(&$s) { 
    $right = strlen($s) - 1;
    $left = 0;
   
    while ($left < $right) { 
        if (!ctype_alpha($s[$left])) {
            $left++; 
        }
        else if (!ctype_alpha($s[$right])) {
                $right--; 
             }
             else { 
                $tmp = $s[$left];
                $s[$left] = $s[$right];
                $s[$right] = $tmp;
                $left++; 
                $right--; 
             } 
    } 
} 
   
$s = "a#b$%c&*(def!"; 
 
reverse($s); 
     
echo $s;
 

  
/*
run:
       
f#e$%d&*(cba!

*/

 



answered Jun 20, 2019 by avibootz

Related questions

...