简体   繁体   English

PHP检测可变长度字符串包含1以外的任何字符

[英]PHP detect variable length string contains any character other than 1

Using PHP I sometimes have strings that look like the following: 使用PHP时,有时会出现如下所示的字符串:

  • 111 111
  • 110 110
  • 011 011
  • 1111 1111
  • 0110012 0110012

What is the most efficient way (preferably without regex) to determine if a string contains any character other then the character 1 ? 确定字符串是否包含除字符1 任何 其他字符的最有效方法(最好是不使用正则表达式)是什么?

Here's a one-line code solution that can be put into a conditional etc.: 这是可以放在条件等中的单行代码解决方案:

strlen(str_replace('1','',$mystring))==0

It strips out the "1"s and sees if there's anything left. 它去除“ 1”并查看是否还有剩余。

User Don't Panic commented that str_replace could be replaced by trim : 用户不要恐慌评论说str_replace可以用trim代替:

strlen(trim($mystring, '1'))==0

which removes leading and trailing 1s and sees if there's anything left. 它会删除前导和尾随的1,并查看是否还有剩余。 This would work for the particular case in OP's request but the first option will also tell you how many non-"1" characters you have (if that information matters). 这将适用于OP请求中的特定情况,但第一个选项还将告诉您您拥有多少个非“ 1”字符(如果该信息很重要)。 Depending on implementation, trim might run slightly faster because PHP doesn't have to check any characters between the first and last non-"1" characters. 根据实现的不同, trim可能会运行得更快一些,因为PHP不必检查第一个和最后一个非“ 1”字符之间的任何字符。


You could also use a string like a character array and iterate through from the beginning until you find a character which is not =='1' (in which case, return true) or reach the end of the array (in which case, return false). 您也可以使用字符串,例如字符数组,并从头开始遍历直到找到不是=='1'的字符(在这种情况下,返回true)或到达数组的末尾(在这种情况下,返回return假)。


Finally, though OP here said "preferably without regex," others open to regexes might use one: 最后,虽然OP这里说的“最好不用正则表达式,”别人开的正则表达式可以使用一个:

 preg_match("/[^1]/", $mystring)==1

Another way to do it: 另一种方法是:

if (base_convert($string, 2, 2) === $string) {
    // $string has only 0 and 1 characters.
}

since your $string is basically a binary number, you can check it with base_convert . 由于$string基本上是一个二进制数,因此可以使用base_convert进行检查。

How it works: 这个怎么运作:

var_dump(base_convert('110', 2, 2)); // 110
var_dump(base_convert('11503', 2, 2)); // 110
var_dump(base_convert('9111111111111111111110009', 2, 2)); // 11111111111111111111000

If the returned value of base_convert is different from the input, there're something other characters, beside 0 and 1 . 如果base_convert的返回值与输入的值不同,则在01旁边还有其他字符。

If you want checks if the string has only 1 characters: 如果要检查字符串是否只有 1字符:

if(array_sum(str_split($string)) === strlen($string)) {
    // $string has only 1 characters.
}

You retrieve all the single numbers with str_split , and sum them with array_sum . 您使用str_split检索所有单个数字,然后将它们与array_sum If the result isn't the same as the length of the string, then you've other number in the string beside 1 . 如果结果与字符串的长度不同,则字符串中除1还有其他数字。

Another option is treat string like array of symbols and check for something that is not 1 . 另一个选择是将字符串像符号数组一样对待,并检查是否为非1 If it is - break for loop: 如果是-打破for循环:

for ($i = 0; $i < strlen($mystring); $i++) {
   if ($mystring[$i] != '1') {
       echo 'FOUND!';
       break;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM