Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查单词是否由连续的字母字符组成_Php_Alphabetical - Fatal编程技术网

Php 检查单词是否由连续的字母字符组成

Php 检查单词是否由连续的字母字符组成,php,alphabetical,Php,Alphabetical,我将一个句子作为输入,如下所示: abcd 01234 87 01235 接下来,我必须检查每个单词,看看它的字符在字母表中是否是连续的。输出如下所示: abcd 01234 嗯,01235包含连续字符,但整个单词也包含非连续字符(35),因此它不会打印在屏幕上 到目前为止,我写了以下内容: function string_to_ascii($string) { $ascii = NULL; for ($i = 0; $i < strlen($string); $i++)

我将一个句子作为输入,如下所示:

abcd 01234 87 01235

接下来,我必须检查每个单词,看看它的字符在字母表中是否是连续的。输出如下所示:

abcd 01234

嗯,
01235
包含连续字符,但整个单词也包含非连续字符(
35
),因此它不会打印在屏幕上

到目前为止,我写了以下内容:

function string_to_ascii($string)
{
    $ascii = NULL;

    for ($i = 0; $i < strlen($string); $i++)
    {
        $ascii[] =  ord($string[$i]);
    }

    return($ascii);
}


$input = "abcd 01234 87 01235";
//first, we split the sentence into separate words
$input = explode(" ",$input);
foreach($input as $original_word)
{
    //we need it clear
    unset($current_word);

    //convert current word into array of ascii chars
    $ascii_array = string_to_ascii($original_word);

    //needed for counting how many chars are already processed
    $i = 0;

    //we also need to count the total number chars in array
    $ascii_count = count($ascii_array);

     //here we go, checking each character from array
     foreach ($ascii_array as $char)
     {
        //if IT'S THE LAST WORD'S CHAR
        if($i+1 == $ascii_count)
        {
            //IF THE WORD HAS JUST 1 char, output it
            if($ascii_count == 1)
            {
                $current_word  .= chr($char);
            }
            //IF THE WORDS HAS MORE THAN 1 CHAR
            else
            {
                //IF PREVIOUS CHAR CODE IS (CURRENT_CHAR-1)  (CONSECUTIVE, OUTPUT IT)
                if(($char - 1) == $ascii_array[($i-1)])
                {
                    $current_word .=chr($char);
                }

            }
        }
        //IF WE AREN'T YET AT THE ENDING
        else
        {
            //IF NEXT CHAR CODE IS (CURRENT_CHAR+1) (CONSECUTIVE, OUTPUT IT)
            if(($char + 1) == ($ascii_array[($i+1)]))
            {
                $current_word .=chr($char);
            }

        }

        $i++;
     }

    //FINALLY, WE CHECK IF THE TOTAL NUMBER OF CONSECUTIVE CHARS is the same as THE NUMBER OF CHARS
    if(strlen($current_word) == strlen($original_word))
    {
        $output[] = $current_word;
    }

}
//FORMAT IT BACK AS SENTENCE
print(implode(' ',$output));
函数字符串到ascii($string)
{
$ascii=NULL;
对于($i=0;$i
但也许还有另一种方法,更简单

很抱歉拼写错误

这很有效

$str = 'abcd 01234 87 01235';

$words = explode(' ', $str);

foreach($words as $key => $word) {
    if ($word != implode(range($word[0], chr(ord($word[0]) + strlen($word) - 1)))) {
       unset($words[$key]);
    }
}

echo implode(' ', $words);

基本上,它获取每个单词的第一个字符,并创建字符范围,如果单词由连续字符组成,则该范围将是值

然后它进行一个简单的字符串比较

要获得更高性能的版本

$str = 'abcd 01234 87 01235';

$words = explode(' ', $str);

foreach($words as $key => $word) {

    foreach(str_split($word) as $index => $char) {
      $thisOrd = ord($char); 
      if ($index > 0 AND $thisOrd !== $lastOrd + 1) {
         unset($words[$key]);
         break;
      }
      $lastOrd = $thisOrd;
    }

}

echo implode(' ', $words);


这两个示例都依赖于顺序字符的顺序。ASCII就是这种情况,但我不确定其他字符。

@alex:有没有办法让UTF-8编码字符可以使用这种方式?俄语字母是特定的,它们的代码点是连续的吗?如果不是,你可以建立一组你认为连续的代码点,并与之进行比较。@亚历克斯,谢谢你的建议。我会尝试,如果在某个地方卡住了,请回到SO:)