PHP, String Data Cleaner, Remove All Characters From String Except Letters and Numbers

function cleaner($mytext) {The function then would only allow capital or
//CLEAN OUT ALL NON-ALPHA NUMERICALlowercase letters, no numbers.
CHARACTERSThe next step in the function is to trim the string
$mytext = ereg_replace("[^A-Za-z0-9]", "",which removes all leading and proceeding white
$mytext);blank spaces from the string. Trimming space off
//REMOVE BEGINNING AND ENDING SPACESof a string is very useful for data cleansing, users
$mytext = trim($mytext);return $mytextaren't always aware the put an extra space at
}the end of their input on accident, so trimming off
//USEAGEthose spaces is a great way to make things
$string = "This is some text and numbers 12345more user friendly.
and symbols!£$%^&";echoThe last item that is in the function is the return
$new_stringstatement where the function returns the value
In this PHP function the first thing that we do isof the $mytext variable after all the data
use Regular Expressions or (REGEX) to removecleansing work has been applied.
anything from the $mytext variable that is not aBy investing some time in to learning Regular
capital or lowercase letter. This is done byExpressions (REGEX) you can easily expand the
declaring "A-Z" and "a-z", respectively. We are alsofunction to be much more granular rather than
including "0-9" so that numbers are also allowed.the 'broad sword' that it is now. You could easily
You could modify this function and remove themodify the function to look for one specific
"0-9" and then the function would removecharacter rather than the 'remove all but' way
numbers in addition to everything else it removes.that I have designed it.