Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip down everything, except alphanumeric and European characters in PHP

I am working on validating my commenting script, and I need to strip down all non-alphanumeric chars except those used in Western Europe.

My plan is to regex out all non-alphanumeric characters with:

preg_replace("/[^A-Za-z0-9 ]/", '', $string);

But that so far strips out all European characters and a £ sign, so "Café Rouge" becomes "Caf Rouge".

How can I add an array of Euro chars to the above regex.

The array is:

£, €, 
á, à, â, ä, æ, ã, å,
è, é, ê, ë,
î, ï, í, ì,
ô, ö, ò, ó, ø, õ,
û, ü, ù, ú,
ÿ,
ñ,
ß

I use UTF-8

SOLUTION:

$comment = preg_replace('/[^\p{Latin}\d\s\p{P}]/u', '', $comment);

and

$name = preg_replace('/[^\p{Latin}]/u', '', $name);

$name aslo removes punctuation marks and spaces

Thanks for quick replies

like image 910
Koffeehaus Avatar asked Dec 15 '22 17:12

Koffeehaus


1 Answers

preg_replace('/[^\p{Latin}\d ]/u', '', $str);
like image 158
Ωmega Avatar answered Jan 11 '23 22:01

Ωmega