这似乎很明显,但我找不到办法做到这一点
我认为甚至还有一个常规的PHP函数可以做到这一点,但即便是这样,在经过1.5小时的密集Google搜索之后,它仍然保持隐藏状态。
“youlookgreatbcdetoday” =>其中有“bcde”...所以必须返回真实的
“youlookgreatklmtoday” =>只有“klm”...所以必须返回false
“youlookgreattoday” =>其中没有按字母顺序排列的序列,因此返回false
免责声明:我希望我已经有一些代码可以告诉你,但我确实还没有任何内容。
我唯一能想到的就是将字符串分开在一个数组中,并在阵列上做一些魔术......但即便如此,我也被卡住了。
希望你们中的一个人能救我:)
答案 0 :(得分:17)
所以,让我们从一个使用循环和计数器的简单实现开始(仅用于增加):
function hasOrderedCharactersForward($string, $num = 4) {
$len = strlen($string);
$count = 0;
$last = 0;
for ($i = 0; $i < $len; $i++) {
$current = ord($string[$i]);
if ($current == $last + 1) {
$count++;
if ($count >= $num) {
return true;
}
} else {
$count = 1;
}
$last = $current;
}
return false;
}
那么,它是如何运作的?基本上,它循环遍历,并检查字符的ord
(ascii编号)是否比之前的字符多一个。如果是这样,它会增加计数参数。否则,它将其设置为1(因为我们已经处理了该字符)。然后,如果$count
大于或等于请求的数量,我们知道我们找到了一个序列,并且可以返回......
所以,现在让我们检查两个方向:
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;
}
现在,abcd
和dcba
...
现在,这是一个更简单的解决方案:
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) {
$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);
}
答案 1 :(得分:4)
<?php
function check($input, $length = 4)
{
$sequence = "abcdefghijklmnopqrstuvwxyz";
$sequence .= substr($sequence, 0, $length - 1);
// abcdefghijklmnopqrstuvwxyz is converted to abcdefghijklmnopqrstuvwxyzabc
for ($i = 0; $i < strlen($sequence) - $length; $i++) {
// loop runs for $i = 0...25
if (strpos($input, substr($sequence, $i, $length)) !== false) {
echo sprintf('"%s" contains "%s"' . PHP_EOL, $input, substr($sequence, $i, $length));
return true;
}
}
echo sprintf('"%s" is OK' . PHP_EOL, $input);
return false;
}
check("youlookgreatbcdetoday"); // "youlookgreatbcdetoday" contains "bcde"
check("youlookgreatklmtoday"); // "youlookgreatklmtoday" is OK
check("youlookgreattoday"); // "youlookgreattoday" is OK
check("youlookgreattodayza"); // "youlookgreattodayza" is OK
check("youlookgreattodayzab"); // "youlookgreattodayzab" contains "yzab"
答案 2 :(得分:4)
更少的循环和条件!
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;
}
答案 3 :(得分:2)
这就是我提出的:
/**
* @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));
处理我扔过的任何字符串,你也可以选择你想要计算的字符数! Yey!
答案 4 :(得分:1)
您可以尝试使用PHP的ord()
来获取每个字符的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;
}
这个函数将做的是,它将逐字符遍历字符串。它将使用ord()
来获取当前字符的ASCII值,并将其与之前的字符的ASCII值进行比较。如果它们是顺序的,正向或反向,它会增加一个计数器。当计数器点击4
时,它会返回true
!。
这将匹配正向和反向序列,以及忽略大小写。因此,abcd
将匹配,aBcD
将,以及DcBa
,以及其他任何人!
答案 5 :(得分:1)
这是我提出的一个简单的解决方案:
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";
输出是STR GOOD和STR3 GOOD。 $minMatchLength
是一行中必须为函数返回true的字符数。 (“testing”具有“st”,但长度为3,因此返回false。
修改强>
我更新了它以检查“AbCdE”,因为仅ord
仅对此无效。
答案 6 :(得分:1)
字符的不等式比较确实隐含地使用了ord()值。这是一个简单的脚本,可以调整(特别是对于不区分大小写):
<?php
$string = "thisabcdef";
function hasSequence($string, $sequence_length = 3) {
$num_in_order = 0;
for($i = 1; $i < strlen($string); $i++) {
if($string[$i] > $string[$i-1]) {
$num_in_order++;
} else {
$num_in_order = 0;
}
if($num_in_order >= $sequence_length) {
return TRUE;
}
}
return FALSE;
}
if(hasSequence("testabcd")) {
echo "YUP";
} else {
echo "NOPE";
}
echo "\n";
答案 7 :(得分:1)
也许过于简单?如果您不想使用大小写,请改用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;
}
用strrev()
来窃取IRCMaxwell的观察结果:
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 ||
strpos(strrev($alpha), $char) !== false)) {
$matches++;
}
if ($matches == $depth) return true;
}
return false;
}
答案 8 :(得分:1)
preg_match('/ ((?=ab|bc|cd|de|ef|fg|gh).) {2,} /smix', "testabc")
您显然需要填写连续字母列表。而{2,}
只是探测范围内至少三个字母。
答案 9 :(得分:1)
这是我的看法:
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;
}
它针对$string
个连续字母的任何序列检查$length
。
checkConsecutiveness('1@abcde1', 5) // returns true;
checkConsecutiveness('1@abcd1', 5) // returns false;
注意确保当前char在97-122范围内,因为勾选“[ASCII#96]和打开大括号{[ASCII#123]可能会导致误报。
答案 10 :(得分:0)
你可以这样做(ASCII码与字母表一致):
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
答案 11 :(得分:0)
我的两分钱:
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;
}
答案 12 :(得分:-1)
尝试此功能:
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;
}