2010-03-16 14:14


  • validation
  • csv
  • php

Currently I am using ctype_alnum to check for alphanumeric type in csv file. But one of my data has white spaces between them and so ctype_alnum gives me false.

Example: ctype_alnum("Here is 23"); 

So my question is how can I check for white spaces in string in php ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douxingsuo8809 douxingsuo8809 11年前

    You can use:

    // returns true if $str has a whitespace(space, tab, newline..) anywhere in it.
    function has_whitespace($str) {
      return preg_match('/\s/',$str);

    Or you can write you own function to check if a string contains only alphabets,digits,spaces:

    // returns true if $str has nothing but alphabets,digits and spaces.
    function is_alnumspace($str){
      return preg_match('/^[a-z0-9 ]+$/i',$str);
    点赞 评论 复制链接分享
  • duanqiu2064 duanqiu2064 11年前

    So you want to check if a string consists of a letter (a-z) ignoring case (A-Z) or horizontal white space (space(0x20) and tab(0x09))? That would be:

    if (preg_match('/^[a-z\h]+$/i', $string)) {
    点赞 评论 复制链接分享