By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,053 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,053 IT Pros & Developers. It's quick & easy.

Stripping Data Help Needed

P: n/a
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...

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

On 12-Oct-2003, Ralph Freshour <ra***@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);

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #2

P: n/a
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"
<us***********@nospam.com> wrote:

On 12-Oct-2003, Ralph Freshour <ra***@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);


Jul 17 '05 #3

P: n/a

On 12-Oct-2003, Ralph Freshour <ra***@primemail.com> wrote:
On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
<us***********@nospam.com> wrote:

On 12-Oct-2003, Ralph Freshour <ra***@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);

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


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 ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #4

P: n/a
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"
<us***********@nospam.com> wrote:

On 12-Oct-2003, Ralph Freshour <ra***@primemail.com> wrote:
On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
<us***********@nospam.com> wrote:
>
>On 12-Oct-2003, Ralph Freshour <ra***@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);

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


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().


Jul 17 '05 #5

P: n/a

On 12-Oct-2003, Ralph Freshour <ra***@primemail.com> wrote:
On Mon, 13 Oct 2003 02:30:14 GMT, "Tom Thackrey"
<us***********@nospam.com> wrote:

On 12-Oct-2003, Ralph Freshour <ra***@primemail.com> wrote:
On Mon, 13 Oct 2003 01:25:34 GMT, "Tom Thackrey"
<us***********@nospam.com> wrote:

>
>On 12-Oct-2003, Ralph Freshour <ra***@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);
How would I handle special characters like single and double quotes?
forward and backward slashes?


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().

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


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 ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #6

P: n/a
Ralph Freshour 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?

Thanks...


maybe you need striptags (from php)?

--
marius

Free your mind; the rest will follow.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.