5

I am no PHP expert. I am looking for the PHP equivalent of isLetter() in Java, but I can't find it. Does it exist?

I need to extract letters from a given string and make them lower case, for example: "Ap.ér4i5T i6f;" should give "apéritif'. So, yes, there are accentuated characters in my strings.

Jérôme Verstrynge
  • 51,859
  • 84
  • 263
  • 429
  • `strtolower` will give you the lower-case string, but you'll likely need a regex to parse the input string and just get the alpha characters from it. – Brian Driscoll Mar 15 '12 at 14:23

4 Answers4

7

ctype_alpha().

Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
  • 2
    Don't forget to use `setlocale()` because in the default C locale, only [a-zA-Z] will be considered as alphabetic characters (no accentuated characters) – SirDarius Mar 15 '12 at 14:25
2

In addition to regex / preg_replace, you can also use strtoupper($string) and strtolower($string), if you need to universally upper-case a string. As Konrad mentioned, preg_replace is probably your best bet though.

http://php.net/manual/en/function.strtoupper.php

http://www.php.net/manual/en/function.strtolower.php

DACrosby
  • 10,150
  • 3
  • 34
  • 49
1

In PHP (and in Java) you wouldn’t use isLetter to implement it, you’d rather replace all characters that aren’t letters using a regular expression:

echo preg_replace('/\P{L}/', '', input);

Loop up the documentation of preg_replace and the regex pattern syntax desciption, in particular the relevant Unicode character classes.

Konrad Rudolph
  • 482,603
  • 120
  • 884
  • 1,141
0

You could probably use the php-slugs source code, with appropriate modifications.

Cacovsky
  • 2,445
  • 3
  • 19
  • 26