【问题标题】:PHP: How can I find all possible sequences of any length within a string?PHP:如何在字符串中找到任意长度的所有可能序列?
【发布时间】:2016-06-20 15:46:06
【问题描述】:

我想获取用户输入(字符串)并将其转换为所有可能长度的单词组合(数组)列表,但只保留按顺序排列的组合。

所以下面 PHP 中列出的数组最终会变成:

$newArray = ["this","string","will","be","chopped","up","this string","will be","be chopped","chopped up","this string will","string will be","be chopped up"];

我的代码(不工作)如下:

PHP

$str = "This string will be chopped up.";
$str = strtolower($str);
$strSafe = preg_replace("/[^0-9a-z\s-_]/i","",$str);
$array = explode(" ", $strSafe);
$newArray = [];
$newArrayEntry = [];

for($counter=0; $counter < COUNT($oneword); $counter++) {

    // List single words - as in array $oneword.
    echo "One word: ";
    echo $oneword[$counter];
    echo "<br />";

    $counterTwo = $counter+1;       

    if($counterTwo <= COUNT($oneword)) {
        $newArrayEntry[COUNT($newArrayEntry)] = $oneword[$counter];
        print "Adding counter One to newArray Entry: ".$oneword[$counter]."<br />";
        for($counterThree=($counter+1); $counterThree < $counterTwo; $counterThree++) {
            $newArrayEntry[COUNT($newArrayEntry)] = $oneword[$counterThree];

            if($counterThree - $counterTwo == 1) {
                $newArrayEntry[COUNT($newArrayEntry)] = $oneword[$counterTwo];
                print "Adding counter Two to newArrayEntry: ".$oneword[$counterTwo]."<br />";
            }
        }
        $newArrayString = join(' ', $newArrayEntry);
        $newArray[COUNT($newArray)] = $newArrayString;
    }
}

【问题讨论】:

  • “字符串将”丢失...
  • 您可以使用 push 将值添加到您的数组中......并且在计数器循环中,您需要另一个计数器,据我所知,它可以从不同的点和不同的地方循环遍历您的基本数组长度。
  • 谢谢梅达!不知道你的意思是什么。
  • 我的意思是您在所需的输出中拥有....."this string","will be"....,但从逻辑上讲,"string will", 应该出现在这两个值之间。

标签: php arrays string loops


【解决方案1】:

您可以使用以下代码:

$str = "This string will be chopped up.";
$str = strtolower($str);
$strSafe = preg_replace("/[^0-9a-z\s-_]/i","",$str);
$array = explode(" ", $strSafe);
$newArray = [];

for ($len = 1; $len <= count($array); $len++) {
    for($start = 0; $start+$len <= count($array); $start++) {
        $newArray[] = implode(" ", array_slice($array, $start, $len));
    }
}   


var_export($newArray);  

输出:

array (
  0 => 'this',
  1 => 'string',
  2 => 'will',
  3 => 'be',
  4 => 'chopped',
  5 => 'up',
  6 => 'this string',
  7 => 'string will',
  8 => 'will be',
  9 => 'be chopped',
  10 => 'chopped up',
  11 => 'this string will',
  12 => 'string will be',
  13 => 'will be chopped',
  14 => 'be chopped up',
  15 => 'this string will be',
  16 => 'string will be chopped',
  17 => 'will be chopped up',
  18 => 'this string will be chopped',
  19 => 'string will be chopped up',
  20 => 'this string will be chopped up',
)

【讨论】:

  • 感谢您的优化,但我还需要读取... array(...(cont.) 'string will', 'string will be', [...] 'will是','将被切碎','将被切碎','被切碎','被切碎','切碎')我想我已经解决了。今晚只是要测试,我会更新我的问题以包含答案。谢谢!
  • 您提到的值都在输出中,或者我不明白您的意思。据我所见,我的代码产生的输出与您的问题一致......另外,请不要在问题中回答,而是发布答案......作为答案。
  • 对不起...我完全误读了这个。我想我把它和另一个问题混在一起了......谢谢! :) (我打算在发帖前阅读关于回答你自己问题的规则......在这里不太精通最佳实践。虽然现在我不需要!)
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 2019-05-10
  • 2018-08-22
  • 1970-01-01
相关资源
最近更新 更多