简体   繁体   中英

PHP extract word that contains special character from a string

I have a string :

$str = " Côte-d'azure ! (3000) limousin - limousine  ";

And I need to extract some words and put them in an array. to get finally :

array (
        0 => "Côte-d'azure",
        1 => "limousin",
        2 => "limousine"
     );

So I tried :

preg_match_all("/[a-zA-Z]+/", $str, $all);

but this ignore the special character ô , ' and -

please any advise ?

Use Unicode mode u and character properties :

preg_match_all('/\p{L}[\p{L}\\\\\'-]+/u', mysql_real_escape_string($str), $all);

This requires one (Unicode) letter and then matches as many other Unicode letters, backslashes, hyphens and apostrophes as possible. If you want other punctuation characters to not separate a word, include it in the character class.

Note that 5 backslashes. Three backslashes are removed when the string is compiled, because two of them escape the backslash following them, and the last one escapes the ' . So the regex engine receives only 2 backslashes. These are interpreted by the regex engine as one literal backslash. Unfortunately there is no way to use less than 4 backslashes to represent one literal backslash when using PHP.

try

if (preg_match('/[^a-zA-Z0-9]+/', $your_string, $matches))
{
  echo '  symbol encountered !!';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM