To support special characters such as ñ, á, é, í, ó, ú, etc.

I am currently using this script to disinfect a block of text ...

function rseo_sanitize($s) {
    $result = preg_replace("/[^a-zA-Z0-9'-]+/", "", html_entity_decode($s, ENT_QUOTES));
    return $result;
}

I would like to add support for a collection of special characters such as ñ, á, é, í, ó, ú, etc.

How can I integrate those (and a large collection of Spanish characters) into preg_replace?

+3
source share
2 answers

You can use /\pL+/uto match all Unicode letter characters.

There is no separate plane for Spanish letters only in PCRE, but you can try:

 /[^\p{Latin}0-9'-]+/u

This includes everything I see in ISO Latin-1 encoding. This covers other European languages, not just Spanish. But otherwise, you really need to specify the necessary letters separately.

+2
source

You must use \wwith modifieru

Example:

/[^\w]+/u
0
source

All Articles