Azazia Software Knowledgebase
Search:     Advanced search
Browse by category:
Contact Us

Regular Expression to Escape all Illegal (non alphanumeric) Characters Except a space

Add comment
Views: 660
Votes: 0
Comments: 0
Posted: 01 Nov, 2009
by: Patridge B.
Updated: 01 Nov, 2009
by: Patridge B.
I created the following functions which I found to be the easiest way to escape all illegal characters.


To escape all illegal (non alphanumeric) characters except a white space.

<?php
function stripChars($string) {
$new_string=trim(ereg_replace("[^a-zA-Z0-9[:space:]]", "", $string));
return $new_string;
}

$result=stripChars("This !is__  a@  test&")
echo $result;
?>

The above would display "This is a test"

To Escape All characters except alphanumeric and an underscore
ereg_replace("[^a-zA-Z0-9_]", "", $string));

To Escape All characters except alphanumeric and an underscore and a dot (period)
ereg_replace("[^a-zA-Z0-9_.]", "", $string));


To Escape All characters except numeric and an underscore and a dot (period)
ereg_replace("[^0-9_.]", "", $string));
Others in this Category
document VI editor Backup (VIB) Utility
document E:Lists directory /var/lib/apt/lists/partial is missing.
document Quick One liners to test a string whether or not it is an Integer, Alphanumeric, or Character
document Using Awk to display a directory listing on files with spaces until the end of line
document UNIX Command Line way to convert HEX ascii codes to Text
document Restoring the cursors last position in the VI (VIM) editor
document Postfix/Sendmail Errors: NOQUEUE: reject: RCPT .... Relay access denied DSN: Service unavailable
document Display path, user name, and host name next to the cursor in the .bashrc or .bash_profile
document Little script to make process searching easier
document Create an SFTP account that will restrict the user only to their destination home directory so they cannot change directory (cd) or view any other files outside of that directory.
document Recompile apache2 to support LDAP on Solaris 10
document SVN Solaris 10: ld: fatal: Symbol referencing errors. No output written to .libs/svn
document SVN Solaris 10: ld.so.1: svnversion: fatal: libssl.so.0.9.8: open failed: No such file or directory
document Solaris 10: commit failed: svn: Couldn't perform atomic initialization
document Creating a UNIX sparse file on an Isilon system



RSS