| function cleaner($mytext) { | | | | The function then would only allow capital or |
| //CLEAN OUT ALL NON-ALPHA NUMERICAL | | | | lowercase letters, no numbers. |
| CHARACTERS | | | | The 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 SPACES | | | | of a string is very useful for data cleansing, users |
| $mytext = trim($mytext);return $mytext | | | | aren't always aware the put an extra space at |
| } | | | | the end of their input on accident, so trimming off |
| //USEAGE | | | | those spaces is a great way to make things |
| $string = "This is some text and numbers 12345 | | | | more user friendly. |
| and symbols!£$%^&";echo | | | | The last item that is in the function is the return |
| $new_string | | | | statement where the function returns the value |
| In this PHP function the first thing that we do is | | | | of the $mytext variable after all the data |
| use Regular Expressions or (REGEX) to remove | | | | cleansing work has been applied. |
| anything from the $mytext variable that is not a | | | | By investing some time in to learning Regular |
| capital or lowercase letter. This is done by | | | | Expressions (REGEX) you can easily expand the |
| declaring "A-Z" and "a-z", respectively. We are also | | | | function 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 the | | | | modify the function to look for one specific |
| "0-9" and then the function would remove | | | | character rather than the 'remove all but' way |
| numbers in addition to everything else it removes. | | | | that I have designed it. |