Old 28-07-2006, 13:34   #1 (permalink)
Skidzy McFergus
Registered User
 
Join Date: Nov 2005
Posts: 57
php remove punctuation

Hi, i was wondering if there was a php function that removes all punctuation from a string of text. I can filter out full stops, commas etc using regex but thought surely there must be a function to do all punctuation. Have looked through the manual but could find anything.
  Reply With Quote
Old 28-07-2006, 15:00   #2 (permalink)
MikeMackay
Shun the non-believer
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Essex, UK
Posts: 1,922
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
What about building a regexp that only allows [a-z][0-9] and spaces (if you want spaces allowed). Get it to delete everything else from the string, that should take care of removing absolutely everything.

Be careful with it though.

- Mike
  Reply With Quote
Old 01-08-2006, 11:32   #3 (permalink)
xml
Senior Member
 
Join Date: Sep 2004
Posts: 149
Skidzy McFergus,
Do you anticipate unicode characters in the string?

If so try this:
$cleanUnicodeStr = trim(preg_replace('#[^\p{L}\p{N}]+#u', ' ', $unicodeStr));

This will also clean up shitty unicode punctuation like those smily faces, musical quotations and stars etc, whilst at the same time allow for Japanese symbols and other such characters.
__________________
  Reply With Quote
Old 01-08-2006, 11:48   #4 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,208
or
Code:
$text = preg_replace('/\W/', ' ', $text);
  Reply With Quote
Old 01-08-2006, 11:50   #5 (permalink)
xml
Senior Member
 
Join Date: Sep 2004
Posts: 149
Does that support unicode smallbeer?
__________________
  Reply With Quote
Old 01-08-2006, 12:15   #6 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,208
I'm not exactly sure what you mean by support, but I guess you'd want to use a function that supports multibyte characters.

http://uk2.php.net/mb_ereg_replace
  Reply With Quote
Old 01-08-2006, 13:42   #7 (permalink)
xml
Senior Member
 
Join Date: Sep 2004
Posts: 149
preg_replace supports unicode, by using the "u" modifier.

Without setting the "u" unicode modifier the string will be interpreted as a string of bytes and will not recognise multibyte unicode characters as individual items to be matched. Obviously cutting part way thru a multibyte character will fuck up the string royally causing a few heart aches on a internationalised application.
__________________
  Reply With Quote
Old 03-08-2006, 20:02   #8 (permalink)
Skidzy McFergus
Registered User
 
Join Date: Nov 2005
Posts: 57
thanks for all that, will process in due course.
  Reply With Quote
Old 22-11-2010, 16:50   #9 (permalink)
WebDesignCanada
Registered User
 
Join Date: Nov 2010
Posts: 1
remove punctuation

I would use the regular expression:
$string = preg_replace('/[^a-zA-Z0-9 ]/','',$string);
  Reply With Quote
Old 22-11-2010, 16:59   #10 (permalink)
Pete Nice
A to the K
 
Pete Nice's Avatar
 
Join Date: Apr 2003
Posts: 4,009
Good choice to re-open a 4 year old thread with your first post. 11 more and then your link I presume?
  Reply With Quote
Old 21-02-2011, 19:34   #11 (permalink)
ceyhunkarbeyaz
Registered User
 
Join Date: Feb 2011
Posts: 1
Quote:
Originally Posted by WebDesignCanada
I would use the regular expression:
$string = preg_replace('/[^a-zA-Z0-9 ]/','',$string);

Thank you very much
  Reply With Quote
Old 21-02-2011, 19:56   #12 (permalink)
moz
Steady on, old chap.
 
moz's Avatar
 
Join Date: Sep 2010
Location: 1960's.
Posts: 2,392
Fuck - another nugget.
__________________
For those of you looking for a good CMS, try:

Ionize CMS - nothing to do with me but a fucking brilliant, comprehensive CMS which uses a MYSQL database.
GetSimple CMS - still nothing to do with me but this comprehensive CMS doesn't need a database.
Monstra CMS - again, nothing to do with me but this is a clean and simple, lightweight CMS.
Sitecake - you guessed it (nowt to do with me) but this CMS is a simple, neat inline content editing system.
  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


Contact Us - Web Design Forums - Archive
vBulletin © 2000-2009 Jelsoft Enterprises Limited.
Search Engine Optimization by vBSEO 3.0.0 RC8