doutongya8378 2015-06-08 05:45
浏览 42
已采纳

我如何计算字符串中的char并使用PHP进行验证

I want to validate a string in such a way that in must have 2 hypens(-)

Strings input : (for eg)

B405Q-0123-0600

B405Q-0123-0612

R450Y-6693-11H0

R450Y-6693-11H1
  • 写回答

6条回答 默认 最新

  • 普通网友 2015-06-08 06:02
    关注

    Make use of substr_count function like this

    <?php
     echo substr_count( "B405Q-0123-0600", "-" )."
    ";
     echo substr_count( "B405Q01230600", "-" )."
    ";
    ?>
    

    Will Result

    2
    0
    

    Validate like this

    if(substr_count( $some_string, "-" ) == 2)
    {
           echo 'true';
           // do something here
    }else
    {
           echo 'false validation failed';
           // some error handling
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?