Strip punctuation from a string with regex using PHP

Handy little snippet.

// To keep letters & numbers
$string = preg_replace('/[^a-z0-9]+/i', '_', $string);

// To keep letters only
$string = preg_replace('/[^a-z]+/i', '_', $string); 

// To keep letters, numbers & underscore
$string = preg_replace('/[W]+/', '_', $string);

Source

Comments

  • Anonymous says:

    dgfdgfd

  • Torie says:

    akhirnya CR7 bisa membuktikan dan membalas dendam pada spanyol. Saya malah heran dengan spanyol, karena pada peannaditgrn persahabatan yang resmi seperti ini bisa kalah. Hal serupa dialami spanyol yang harus kalah melawan argentina dalam pertandingan persahabatan resmi FIFA

  • Ã… dette høres ut som den perfekte kveld ja;o) Du fÃ¥r hilse sÃ¥ mye….Ellers mÃ¥ jeg si at du har en utrolig vakker header…..og selvfølgelig flotte bilder som alltid. Kos dere videre…Klem fra Maliva

  • may all ur remaining hair under ur lock extensions fall out may u have alopecia and folliculitis for the rest of ur days alicia banks ya buceta breaf moron may all the people with whom u wish to share intimacy be repulsed by ur visageenjoy urself in hell chrick!

  • http://www./ says:

    พูดว่า:Hey there, I just hopped over to your web site thru StumbleUpon. Not somthing I might generally browse, but I enjoyed your thoughts none the less. Thank you for making some thing worth reading.

  • http://www./ says:

    Hi Meiling,This is Wing from Oisix, a Japanese food delivery company. May I have your email so that I can discuss with you some matters?here is the website:hk.oisix.comThanks!Regards,WingOisix HK

  • http://www./ says:

    Mulțumesc, Ovi. Acțiunea cu baloanele implica niște mii sau zeci de mii de baloane (nu mai țin minte exact) pe care lumea scria urări de An Nou și desena diverse chestii, urmând a fi lansate pe apă în noaptea de Revelion (2009). Au fost acolo și urările noastre.

  • http://www./ says:

    Hallo,lang ists her, dass du diesen Blogeintrag geschrieben hast. Aber – hast du denn nun eigenlich so ein open-source Programm gefunden? Ich bin seit geraumer Zeit auf der Suche nach genau so einem, finde aber wie du nur grosse abgeschlossene Systeme oder teure kommerzielle…GeliBo

Club-Mate, the beverage → club-mate.fi