TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-20-2009, 01:56 AM   #1 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default Using regex

The following code, the way I understand it, is supposed to strip all punctuation characters from the string. From what I've found on the web, a space is not one of those characters. But the code below outputs "abstractmodern." Would someone please explain how I can strip all special characters except the spaces?
PHP Code:
$pattern "([[:punct:]])"
$str "abstract modern"
$str ereg_replace($pattern''$str)); 
echo 
$str
benton is offline  
Reply With Quote
Old 07-20-2009, 03:25 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Use this range [!-@]

This is because ranges go by ASCII value, where ! is the first punctuation (after space) and @ is the last.
__________________

Village Idiot is offline  
Reply With Quote
Old 07-20-2009, 03:32 AM   #3 (permalink)
The Wanderer
 
EGYG33K's Avatar
 
Join Date: Jul 2009
Posts: 5
Thanks: 0
EGYG33K is on a distinguished road
Smile

PHP Code:
echo ereg_replace("[^[:alpha:],[:digit:],_,[:blank:]]","",'this is 123 $ *&^_ text test'); 
EGYG33K is offline  
Reply With Quote
Old 07-20-2009, 04:38 AM   #4 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

As a side note, it's best just to stop using the ereg_* function and it's POCL ( I think it is ) engine because as of 5.3.0 it's going to through an E_DEPRECIATED error. Use the preg_* family
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 07-20-2009, 03:14 PM   #5 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

Thanks for the suggestions but they don't seem to be working. I changed the input string to more properly illustrate the problem. So given this string
PHP Code:
$str '#!$abstract modern-fields'
it should output
Quote:
abstract modern-fields
If I use this
PHP Code:
echo ereg_replace("[^[:alpha:],[:digit:],_,[:blank:]]","",$str).'<br>'
It displays
Quote:
abstract modernfields
And if I use this
PHP Code:
ereg_replace("[[!-@],[:punct:]]","",$str).'<br>'
It displays
Quote:
#!$abstract modern-fields
The #!S are all listed as characters in :punct: so those should be stripped. The first method does that but it also removes the hyphen. The second doesn't remove the punctuation but handles the hyphen correctly. Would someone point out my mistake, please?
benton is offline  
Reply With Quote
Old 07-20-2009, 04:27 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Do you want to use a whitelist or a blacklist. The former would remove all characters except a specific set, e.g. allow only alphanumeric and hyphen characters. The latter, a blacklist, would remove only specific characters, e.g. !@#$%^&*().

It is also advised to use the PCRE functions (preg_replace) since the POSIX family of functions (ereg_*, split, etc.) is deprecated.

With regards to why ereg_replace("[[!-@],[:punct:]]","",$str) won't work, it's just how you've constructed the character set. There shouldn't be a set of square brackets around !-@. Note that that range includes the dash/hyphen character - so that will be removed.

Back to white- and blacklists, here are a few examples:
Only allow alphanumeric (case insensitive) and hyphen characters
PHP Code:
echo preg_replace('/[^a-z0-9-]/i'''$str); 
Only remove !@#$%^&*()+= characters
PHP Code:
echo preg_replace('/[!@#$%^&*()+=]/'''$str); 
Salathe is offline  
Reply With Quote
Old 07-20-2009, 07:52 PM   #7 (permalink)
The Wanderer
 
EGYG33K's Avatar
 
Join Date: Jul 2009
Posts: 5
Thanks: 0
EGYG33K is on a distinguished road
Default

PHP Code:
echo ereg_replace("[^[:alpha:],[:digit:],_,-,[:blank:]]","",$str).'<br>'
EGYG33K is offline  
Reply With Quote
Old 07-23-2009, 05:48 PM   #8 (permalink)
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default

My thanks to everyone. The code is working as expected now and I have a better understanding of how to manipulate the string. I do appreciate it.
benton is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Evaluate my regex pleeze? Aaron General 8 05-05-2008 01:24 AM
Is this a propert URL regex? Aaron Absolute Beginners 2 04-16-2008 05:18 PM
Remembering why I hate Regex RobertK Absolute Beginners 4 01-09-2008 02:37 PM
Filter_var or RegEx? Orc General 4 01-08-2008 03:04 PM
RegEx xenon General 6 12-12-2007 10:38 AM


All times are GMT. The time now is 12:06 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design