Remove all special characters from the string in PHP. The str_replace () method is used to remove all special characters from the given string

#example.php

    <?php

// Function to remove all spacial char 
    function removespecialChar($str) {

        $string = str_replace(' ', '-', $str); // Replaces all spaces with hyphens.
        return preg_replace('/[^A-Za-z0-9-]/', '', $str); 
        return $res;
    }

    // Given string 
    $str = "Example,to remove All Special'Char including any &&$^#^$^;";

    // Function calling 
    $str1 = removespecialChar($str);

    // Printing the result 
    echo $str1;
    ?>

Output:

ExampletoremovetheAllSpecialCharincludingany