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

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