How to remove special character from string in php?

In this post,we will leran you how to remove special character from string in php.

1. Code:

<?php
function removeSpecialCharacters($string){
    $result  = preg_replace('/[^a-zA-Z0-9_ -]/s','', $string);
    return $result;
}

echo removeSpecialCharacters("This - text ! has \\ /allot # of % special % characters");
?>

2. Example:

Input:

"This - text ! has \\ /allot # of % special % characters"

Output:

This - text has allot of special characters