1

I have a similar question to this one

However, I have a less strict requirement.

All I need is to detect if the input string contains any non alphabetic string.

If it does contain non-alphabetic string, then I will select a different font file.

if it contains ONLY alphabetic string, then I will select a font file like AmericanTypeWriter.

By alphabetic string, that would include all kinds of possible symbols such as commas, punctuations, etc.

It is hard to define alphabetic string.

Let me define what is an example of non-alphabetic string.

这是中文

And assuming utf-8 format for the string.

Another way to define: anything that does not fall under non-European language character, is automatically assumed to be alphabetic string.

I need to do this detection in php by the way.

Community
  • 1
  • 1
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282

2 Answers2

1

Use ctype_alnum(string). Documentation Here. This function takes in a string and returns a bool that tells you whether the string contains other characters or not.

You could also use a more complex regex which will work and check for spaces. The following should work.

preg_match("/^[a-zA-Z0-9 ]*$/u", $string) == 1 will do the trick.

hakre
  • 193,403
  • 52
  • 435
  • 836
secretformula
  • 6,414
  • 3
  • 33
  • 56
  • oh that does not work well, because i may have spaces. this function only tests for alphanumeric not including spaces – Kim Stacks Jun 18 '12 at 03:01
  • Sorry for not stating it clearly. Usual punctuation such as commas and exclamation marks fall under the AmericanTypeWriter font so alphabetic string would include it as well. – Kim Stacks Jun 18 '12 at 05:29
  • Based on your use of regex, all i need to do is add in the punctuation symbols. Am I right? preg_match("^[a-zA-Z0-9,\.\-_! ]*$", $string) == 1 – Kim Stacks Jun 18 '12 at 05:30
  • Yes, please acccept answer if it helped you – secretformula Jun 18 '12 at 11:28
  • I have up voted the answer. I will probably edit the answer a bit and accept it AFTER i tried this out in the php app. in the meantime, thank you @secretformula – Kim Stacks Jun 19 '12 at 07:00
  • Works. Thanks. Got another question about PHP here http://stackoverflow.com/q/11101544/80353 – Kim Stacks Jun 19 '12 at 13:14
1

I found a better answer. Inspired by https://stackoverflow.com/a/4923410/80353

header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('utf-8');

function isThisChineseText($text) {
    return preg_match("/\p{Han}+/u", $text);
}
Community
  • 1
  • 1
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282