Connecting Tech Pros Worldwide Help | Site Map

Stripping Data Help Needed

Ralph Freshour
Guest
 
Posts: n/a
#1: Jul 17 '05
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?

Thanks...

Tom Thackrey
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Stripping Data Help Needed



On 12-Oct-2003, Ralph Freshour <ralph@primemail.com> wrote:
[color=blue]
> 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?[/color]

$strippedtext = preg_replace('/[^0-9a-z]/i','',$originaltext);

--
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)
Ralph Freshour
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Stripping Data Help Needed


How would I handle special characters like single and double quotes?
forward and backward slashes?

Thanks...

On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
<use.signature@nospam.com> wrote:
[color=blue]
>
>On 12-Oct-2003, Ralph Freshour <ralph@primemail.com> wrote:
>[color=green]
>> 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?[/color]
>
>$strippedtext = preg_replace('/[^0-9a-z]/i','',$originaltext);[/color]

Tom Thackrey
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Stripping Data Help Needed



On 12-Oct-2003, Ralph Freshour <ralph@primemail.com> wrote:
[color=blue]
> On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
> <use.signature@nospam.com> wrote:
>[color=green]
> >
> >On 12-Oct-2003, Ralph Freshour <ralph@primemail.com> wrote:
> >[color=darkred]
> >> 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?[/color]
> >
> >$strippedtext = preg_replace('/[^0-9a-z]/i','',$originaltext);[/color][/color]
[color=blue]
> 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().

--
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)
Ralph Freshour
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Stripping Data Help Needed


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 {}[]<>

Ralph

On Mon, 13 Oct 2003 02:30:14 GMT, "Tom Thackrey"
<use.signature@nospam.com> wrote:
[color=blue]
>
>On 12-Oct-2003, Ralph Freshour <ralph@primemail.com> wrote:
>[color=green]
>> On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
>> <use.signature@nospam.com> wrote:
>>[color=darkred]
>> >
>> >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]
>[color=green]
>> 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]

Tom Thackrey
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Stripping Data Help Needed



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)
marius ursache
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Stripping Data Help Needed


Ralph Freshour wrote:
[color=blue]
> 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?
>
> Thanks...[/color]

maybe you need striptags (from php)?

--
marius

Free your mind; the rest will follow.
Closed Thread