如果没有条件,我如何计算字符串中的字符?

时间:2017-12-14 23:27:40

标签: php string if-statement count strlen

我有代码示例:

$ou = 'OU=Security Groups,DC=mydomain,DC=local'

$basepath =  '\\mydomain\dfsroot'

$filter = '*501*'

Get-ADGroup -SearchBase $ou -Filter { Name -like $filter } | % {
    $principle = $_.samAccountName
    Get-ChildItem -LiteralPath $basepath -Recurse | % {
        $path = $_.FullName
        ($path | Get-Acl).Access.IdentityReference | % { if ( $_.Value -match $principle ) { Write-Host "$principle has rights to $path" }}
    }
}

我的问题是如何在字符串很大且未在$str = '123456789101112131415'; if(strlen($str) <9 ) { echo "the string is 10"; } elseif(strlen($str) > 9) { echo 'the string is 11'; } elseif(strlen($str) > 21) { echo 'the string is 30'; } elseif(strlen($str) > 31) { echo 'the string is 40'; } 条件中指定

时进行整理

1 个答案:

答案 0 :(得分:1)

计算它!

  1. 致电strlen()
  2. 除以10
  3. 调用floor()删除小数
  4. 乘以10
  5. 检查是否小于10
  6. 以下是一系列测试:

    代码:(Demo

    $strings=[9=>'123456789',10=>'1234567890',11=>'12345678901',19=>'1234567890123456789',20=>'12345678901234567890',21=>'123456789012345678901'];
    foreach($strings as $k=>$str){
        if(($group=floor(strlen($str)/10)*10)<10){  // use arithmetic to find the group, declare and check group in one step
            echo "$k => $str is not upto 10";
        }else{
            echo "$k => $str is in group $group";
        }
        echo "\n";
    }
    

    输出:

    9 => 123456789 is not upto 10
    10 => 1234567890 is in group 10
    11 => 12345678901 is in group 10
    19 => 1234567890123456789 is in group 10
    20 => 12345678901234567890 is in group 20
    21 => 123456789012345678901 is in group 20
    

    OP更新后:

    代码:(Demo

    $strings=[9=>'123456789',10=>'1234567890',11=>'12345678901',19=>'1234567890123456789',20=>'12345678901234567890',21=>'123456789012345678901'];
    foreach($strings as $k=>$str){
        echo "$k => $str is in group ",(floor(strlen($str)/10)+1)*10,"\n";
    }
    

    输出:

    9 => 123456789 is in group 10
    10 => 1234567890 is in group 20
    11 => 12345678901 is in group 20
    19 => 1234567890123456789 is in group 20
    20 => 12345678901234567890 is in group 30
    21 => 123456789012345678901 is in group 30
    

    这是另一种方法,它完全提供了OP要求的内容,但我敢打赌它也“不对”。

    http://sandbox.onlinephpfunctions.com/code/77effe7f68c389bafb1d104a49b7055873e7b038