Php 检查字符串中按字母顺序排列的字符

Php 检查字符串中按字母顺序排列的字符,php,string,Php,String,这似乎很明显,但我找不到一个方法来做到这一点。 我认为甚至有一个常规的PHP函数可以做到这一点,但即使是这个函数,在经过1,5个小时的密集谷歌搜索后,也能很好地隐藏起来。 我想要什么 以字符串作为输入的函数。 检查该字符串具有超过3个字符的字母顺序序列的次数: 如果找到多于3个的序列,则返回true。 例子 “youlookgreatbcdetoday”=>中有“bcde”。。。因此必须返回true “youlookgreatklmtoday”=>里面只有“klm”。。。因此必须返回fals

这似乎很明显,但我找不到一个方法来做到这一点。
我认为甚至有一个常规的PHP函数可以做到这一点,但即使是这个函数,在经过1,5个小时的密集谷歌搜索后,也能很好地隐藏起来。

我想要什么
  • 以字符串作为输入的函数。
  • 检查该字符串具有超过3个字符的字母顺序序列的次数:
  • 如果找到多于3个的序列,则返回true。

例子 “youlookgreatbcdetoday”=>中有“bcde”。。。因此必须返回true
“youlookgreatklmtoday”=>里面只有“klm”。。。因此必须返回false
“youlookgreattoday”=>中没有按字母顺序排列的序列,因此返回false


可能用例
  • 密码强度检查器
  • 文字游戏
免责声明:我希望我已经有一些代码可以向您展示,但实际上我还没有任何代码。
我唯一能想到的是将字符串拆分成一个数组,并对数组执行一些魔术。。。但即便如此,我还是被卡住了。

希望你们中的一个能救我:)

你可以尝试使用PHP获取每个字符的ASCII值,然后逐个字符地迭代,比较每个值以找到序列

这可能有助于:

function checkForSequences($str, $minSequenceLength = 4) {
    $length = strlen($str);
    $sequenceLength = 1;
    $reverseSequenceLength = 1;
    for ($i = 1; $i < $length; $i++) {
        $currChar = ord(strtolower($str[$i]));
        $prevChar = ord(strtolower($str[$i - 1])) + 1;
        if ($currChar == $prevChar) {
            // we have two-letters back to back; increment the counter!
            $sequenceLength++;
            if ($sequenceLength == $minSequenceLength) {
                // we've reached our threshold!
                return true;
            }
            // reset the reverse-counter
            $reverseSequenceLength = 1;
        } else if ($currChar == ($prevChar - 2)) {
            // we have two-letters back to back, in reverse order; increment the counter!
            $reverseSequenceLength++;
            if ($reverseSequenceLength == $minSequenceLength) {
                // we've reached our threshold!
                return true;
            }
            // reset the forward-counter
            $sequenceLength = 1;
        } else {
            // no sequence; reset counter
            $sequenceLength = 1;
            $reverseSequenceLength = 1;
        }
    }
    return false;
}
函数checkForSequences($str,$minSequenceLength=4){
$length=strlen($str);
$sequenceLength=1;
$reverseSequenceLength=1;
对于($i=1;$i<$length;$i++){
$currChar=ord(strtolower($str[$i]));
$prevChar=ord(strtolower($str[$i-1]))+1;
如果($currChar==$prevChar){
//我们有两封背对背的信;增加计数器!
$sequenceLength++;
如果($sequenceLength=$minSequenceLength){
//我们已经到了门槛!
返回true;
}
//重置反向计数器
$reverseSequenceLength=1;
}else if($currChar==($prevChar-2)){
//我们有两个字母背靠背,顺序相反;增加计数器!
$reverseSequenceLength++;
如果($reverseSequenceLength==$MinsSequenceLength){
//我们已经到了门槛!
返回true;
}
//重置正向计数器
$sequenceLength=1;
}否则{
//无顺序;重置计数器
$sequenceLength=1;
$reverseSequenceLength=1;
}
}
返回false;
}
这个函数要做的是,它将一个字符一个字符地遍历字符串。它将使用
ord()
获取当前字符的ASCII值,并将其与前一个字符的ASCII值进行比较。如果它们是按顺序排列的,无论是正向还是反向,它都会增加一个计数器。当反击为
4
时,返回
true

这将匹配正向和反向序列,以及忽略大小写。因此,
abcd
将匹配,
abcd
将匹配,
DcBa
也将匹配

试试这个功能:

function checkForString($string,$l=4){
   $length = strlen($string);
   $result = 0;
   for($i=0;$i<$length-1 && $result<4;$i++){

   if($string[$i+1] == $string[$i]++) $result++;

}
if($result>=4) return true;
else return false;
}
函数checkForString($string,$l=4){
$length=strlen($string);
$result=0;

对于($i=0;$i),我提出了一个简单的解决方案:

function alphaCheck($str){
    $array=str_split(strtolower($str));
    $minMatchLength=3;
    $check=array(ord($array[0]));
    foreach($array as $letter){
        $ascii=ord($letter);
        if($ascii==end($check)+1){
            $check[]=$ascii;
            if(count($check)==$minMatchLength)
                return true;
        }else{
            unset($check);
            $check=array($ascii);
        }
    }
    return false;
}
$str="abcdhello";
$str2="testing";
$str3="abdcefghi";
if(alphaCheck($str))
    echo"STR GOOD";
if(alphaCheck($str2))
    echo "STR2 GOOD";
if(alphaCheck($str3))
    echo "STR3 GOOD";
/**
 * @param string $input Input string
 * @param int $length Length of required sequence
 *
 * @return bool
 */

function look_for_sequence($input, $length) {
    //If length of sequence is larger than input string, no sequence is possible.
    if ($length > strlen($input)) {
        return false;
    }
    //Normalize string, only lowercase
    //(That's because character codes for lowercase and uppercase are different).
    $input = strtolower($input);

    //We loop until $length characters before the end of the string, because after that,
    //No match can be found.
    for ($i = 0; $i < strlen($input) - $length; $i++) {
        //Reset sequence counter
        $sequence = 1;
        //Character under inspection.
        $current_character = ord($input[$i]);
        //Let's look forward, $length characters forward:
        for ($j = $i + 1; $j <= $i + $length; $j++) {
            $next_character = ord($input[$j]);
            //If this next character is actually the sequencing character after the current
            if ($next_character == $current_character+1) {
                //Increase sequence counter
                $sequence++;
                //Reset the current character, and move to the next
                $current_character = $next_character;
                //If $length characters of sequence is found, return true.
                if ($sequence >= $length) {
                    return true;
                }
            }
            //If the next character is no sequencing,
            //break this inner loop and continue to the next character.
            else {
                break;
            }
        }
    }
    return false;
}

var_dump(look_for_sequence("youlookgreatbcdetoday", 4));
输出为STR GOOD和STR3 GOOD。
$minMatchLength
是一行中必须包含的字符数,以便函数返回true。(“testing”有“st”,但长度为3,因此返回false

编辑
我将其更新为同时检查“AbCdE”,因为仅使用
ord
无法解决此问题。

因此,让我们从一个使用循环和计数器的简单实现开始(仅用于增加):

现在,对于
abcd
dcba
,它将返回true

现在,这里有一个简单得多的解决方案:

function hasOrderedCharactersForward($string, $num = 4) {
    $len = strlen($string) + 1;
    $array = array_map(
        function($m) use (&$len) {
            return ord($m[0]) + $len--;
        }, 
        str_split($string, 1)
    );
    $str = implode('_', $array);
    $regex = '#(^|_)(\d+)' . str_repeat('_\2', $num - 1) . '(_|$)#';
    return (bool) preg_match($regex, $str);
}
这就是我们使用的属性,如果我们在每个位置添加一个递减的数字,连续的序列将显示为相同的数字。这就是它的工作原理

这是同样适用于两个方向的理论:

function hasOrderedCharacters($string, $num = 4) {
    $len = strlen($string);
    $count = 0;
    $dir = 1;
    $last = 0;
    for ($i = 0; $i < $len; $i++) {
        $current = ord($string[$i]);
        if ($count == 1 && $current == $last - 1) {
            $count++;
            $dir = -1;
            if ($count >= $num) {
                return true;
            }
        } elseif ($current == $last + $dir) {
            $count++;
            if ($count >= $num) {
                return true;
            }
        } else {
            $count = 1;
            $dir = 1;
        }
        $last = $current;
    }
    return false;
}
function hasOrderedCharacters($string, $num = 4) {
    $i = 0;
    $j = strlen($string);
    $str = implode('', array_map(function($m) use (&$i, &$j) {
        return chr((ord($m[0]) + $j--) % 256) . chr((ord($m[0]) + $i++) % 256);
    }, str_split($string, 1)));
    return preg_match('#(.)(.\1){' . ($num - 1) . '}#', $str);
}

字符的不平等比较会隐式使用ord()值。下面是一个简单的脚本,可以对其进行调整(特别是对于大小写不敏感的情况):


可能太简单了?如果希望不区分大小写,可以使用
stripos()

function abc($test, $depth) {
    $alpha = 'abcdefghijklmnopqrstuvwxyz';
    $matches = 0;
    $length = strlen($test);

    while ($length--) {
        $char = substr($test, $length, $depth);

        if (strlen($char) == $depth && strpos($alpha, $char) !== false) {
            $matches++;
        }

        if ($matches == $depth) return true;
    }

    return false;
}

和(从麦克斯韦的观察中)使用
strev()

您可以这样做(ASCII码按字母顺序排列):


使用以下工具也很简单:


显然,您需要完成连续字母的列表。而
{2,}
只需在一个范围内搜索至少三个字母。

这就是我想到的:

function alphaCheck($str){
    $array=str_split(strtolower($str));
    $minMatchLength=3;
    $check=array(ord($array[0]));
    foreach($array as $letter){
        $ascii=ord($letter);
        if($ascii==end($check)+1){
            $check[]=$ascii;
            if(count($check)==$minMatchLength)
                return true;
        }else{
            unset($check);
            $check=array($ascii);
        }
    }
    return false;
}
$str="abcdhello";
$str2="testing";
$str3="abdcefghi";
if(alphaCheck($str))
    echo"STR GOOD";
if(alphaCheck($str2))
    echo "STR2 GOOD";
if(alphaCheck($str3))
    echo "STR3 GOOD";
/**
 * @param string $input Input string
 * @param int $length Length of required sequence
 *
 * @return bool
 */

function look_for_sequence($input, $length) {
    //If length of sequence is larger than input string, no sequence is possible.
    if ($length > strlen($input)) {
        return false;
    }
    //Normalize string, only lowercase
    //(That's because character codes for lowercase and uppercase are different).
    $input = strtolower($input);

    //We loop until $length characters before the end of the string, because after that,
    //No match can be found.
    for ($i = 0; $i < strlen($input) - $length; $i++) {
        //Reset sequence counter
        $sequence = 1;
        //Character under inspection.
        $current_character = ord($input[$i]);
        //Let's look forward, $length characters forward:
        for ($j = $i + 1; $j <= $i + $length; $j++) {
            $next_character = ord($input[$j]);
            //If this next character is actually the sequencing character after the current
            if ($next_character == $current_character+1) {
                //Increase sequence counter
                $sequence++;
                //Reset the current character, and move to the next
                $current_character = $next_character;
                //If $length characters of sequence is found, return true.
                if ($sequence >= $length) {
                    return true;
                }
            }
            //If the next character is no sequencing,
            //break this inner loop and continue to the next character.
            else {
                break;
            }
        }
    }
    return false;
}

var_dump(look_for_sequence("youlookgreatbcdetoday", 4));
/**
*@param string$输入字符串
*@param int$所需序列的长度
*
*@returnbool
*/
函数查找顺序($input,$length){
//若序列长度大于输入字符串,则不可能有序列。
如果($length>strlen($input)){
返回false;
}
//规范化字符串,仅小写
//(这是因为小写和大写的字符代码不同)。
$input=strtolower($input);
//我们循环到字符串结尾之前的$length字符,因为在此之后,
//找不到匹配项。
对于($i=0;$ifunction abc($test, $depth) {
    $alpha = 'abcdefghijklmnopqrstuvwxyz';
    $matches = 0;
    $length = strlen($test);

    while ($length--) {
        $char = substr($test, $length, $depth);

        if (strlen($char) == $depth && 
            (strpos($alpha, $char) !== false || 
             strpos(strrev($alpha), $char) !== false)) {
            $matches++;
        }

        if ($matches == $depth) return true;
    }

    return false;
}
function check_str_for_sequences($str, $min = 3) {
        $last_char_code = -1;
        $total_correct = 0;
        $str = strtolower($str);

        for($i = 0; $i < strlen($str); $i++) {
            //next letter in the alphabet from last char?
            if(ord($str[$i]) == ($last_char_code + 1)) {
                $total_correct++;

                //min sequence reached?
                if($total_correct >= ($min - 1)) {
                    return TRUE;
                }
            } else {
                $total_correct = 0;
            }

            $last_char_code = ord($str[$i]);
        }

        return FALSE;
    }
$test = 'Lorem ipsum dolor abcsit amet';

echo '----->' . check_str_for_alpha($test); // ----->1
preg_match('/  ((?=ab|bc|cd|de|ef|fg|gh).)  {2,}  /smix', "testabc")
/**
 * @param string $input Input string
 * @param int $length Length of required sequence
 *
 * @return bool
 */

function look_for_sequence($input, $length) {
    //If length of sequence is larger than input string, no sequence is possible.
    if ($length > strlen($input)) {
        return false;
    }
    //Normalize string, only lowercase
    //(That's because character codes for lowercase and uppercase are different).
    $input = strtolower($input);

    //We loop until $length characters before the end of the string, because after that,
    //No match can be found.
    for ($i = 0; $i < strlen($input) - $length; $i++) {
        //Reset sequence counter
        $sequence = 1;
        //Character under inspection.
        $current_character = ord($input[$i]);
        //Let's look forward, $length characters forward:
        for ($j = $i + 1; $j <= $i + $length; $j++) {
            $next_character = ord($input[$j]);
            //If this next character is actually the sequencing character after the current
            if ($next_character == $current_character+1) {
                //Increase sequence counter
                $sequence++;
                //Reset the current character, and move to the next
                $current_character = $next_character;
                //If $length characters of sequence is found, return true.
                if ($sequence >= $length) {
                    return true;
                }
            }
            //If the next character is no sequencing,
            //break this inner loop and continue to the next character.
            else {
                break;
            }
        }
    }
    return false;
}

var_dump(look_for_sequence("youlookgreatbcdetoday", 4));
function checkConsecutiveness($string, $length = 3)
{

    $tempCount = 1; 

    for($i = 0; $i < count($tokens = str_split(strtolower($string)) ); $i++)
    {

        // if current char is not alphabetic or different from the next one, reset counter
        if(
            ord($tokens[$i]) < 97 ||
            ord($tokens[$i]) > 122 ||
            ord($tokens[$i]) != (ord( $tokens[$i+1]) -1)
        ){

            $tempCount = 1;

        }
        // else if we met given length, return true
        else if(++$tempCount >= $length)

            return true;

    }

    // no condition met by default
    return false;

}
checkConsecutiveness('1@abcde1', 5) // returns true;
checkConsecutiveness('1@abcd1', 5) // returns false;
  function alphacheck($str, $i=4)
  {
      $alpha = 'abcdefghijklmnopqrstuvwxyz';
      $len = strlen($str);

      for($j=0; $j <= $len - $i; $j++){
          if(strrpos($alpha, substr($str, $j, $i)) !== false){
              return true;
          }
      }

      return false;
  }
function countOrderedCharacters($str, $count){
    $matches = array();
    preg_replace_callback('/(?=(\w{'.$count.'}))/',function($x) use(&$matches,$count) {
        $seq = $x[1];
        if($count === 1){
            $matches[] = $seq;
            return;
        }
        $dif = ord($seq[1]) - ord($seq[0]);
        if (abs($dif)!=1) return;
        for($i = 0 ; $i < strlen($seq)-1 ; $i++){
            if(ord($seq[$i+1]) - ord($seq[$i]) != $dif) return;
        }
        $matches[] = $seq;

    }, $str);
    return $matches;
}