Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to strip punctuation in php

Tags:

string

php

strip

How can I strip punctuation except for these characters . = $ ' - %

like image 468
Okan Kocyigit Avatar asked Mar 08 '11 14:03

Okan Kocyigit


2 Answers

Here's a neat way to do it:

preg_replace("#[[:punct:]]#", "", $target);
like image 69
Waiyl Karim Avatar answered Sep 30 '22 05:09

Waiyl Karim


Since you need to match some Unicode characters () it would be sensible to use a regular expression. The pattern \p{P} matches any known punctuation, and the assertion excludes your desired special characters from vanishing:

 $text = preg_replace("/(?![.=$'€%-])\p{P}/u", "", $text);
like image 35
mario Avatar answered Sep 30 '22 07:09

mario