On 12-Oct-2003, Ralph Freshour <ralph@primemail.com> wrote:
[color=blue]
> On Mon, 13 Oct 2003 02:30:14 GMT, "Tom Thackrey"
> <use.signature@nospam.com> wrote:
>[color=green]
> >
> >On 12-Oct-2003, Ralph Freshour <ralph@primemail.com> wrote:
> >[color=darkred]
> >> On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
> >> <use.signature@nospam.com> wrote:
> >>
> >> >
> >> >On 12-Oct-2003, Ralph Freshour <ralph@primemail.com> wrote:
> >> >
> >> >> I'm taking text from a textarea object and want to strip out
> >> >> characters other than A-Za-z0-9 before I send the data to MySQL
> >> >> table
> >> >> - is there a function I can use to do this in PHP?
> >> >
> >> >$strippedtext = preg_replace('/[^0-9a-z]/i','',$originaltext);[/color]
> >[color=darkred]
> >> How would I handle special characters like single and double quotes?
> >> forward and backward slashes?
> >>[/color]
> >
> >That will strip them out, too. It will remove everything but letters and
> >numbers from the original string. What the regular expression says is
> >replace every occurance of a character that is not (^) a number (0-9) or
> >a
> >letter (a-z) both upper and lower case (i) with a null ('').
> >
> >If you want to just make the string mysql safe but keep the punctuation
> >use
> >addslashes().[/color][/color]
[color=blue]
> I'm sorry I didn't think this through further Tom - while I wanted
> initially 0-9a-z etc., after using this in a form (it worked really
> neat!!), I realized that I needed to allow additional specific
> characters such as {}[]<>
>[/color]
all you have to do is add the extra characters to the pattern, except that
some of them need to be escaped like () and []
$strippedtext = preg_replace('/[^0-9a-z\[\]\(\)<>]/i','',$originaltext);
see
http://www.php.net/manual/en/function.preg-replace.php http://www.php.net/manual/en/pcre.pattern.syntax.php
--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to
jamesbutler@willglen.net (it's reserved for spammers)