PHP-句子按3个单词分组,然后在分组发生变化时得到第一个出现



我不知道如何解释,但这是代码:

$word = 'The quick brown fox jumps over the lazy dog.';
$word_length = strlen($word);
$space_count = 1;
for ($i=0;$i<=$word_length-1;$i++) 
{


if ($word[$i] == ' ')
{
$space_count++;
}
if ($space_count <= 3) //1st set
{
echo 'set1 ';
}
elseif ($space_count > 3 && $space_count <= 6) //2nd set
{
echo 'set2 ';
}
elseif ($space_count > 6 && $space_count <= 9) //3rd set
{
echo 'set3 ';
}
elseif ($space_count > 9) //4th set
{
echo 'set4 ';
}
echo $word[$i].'<br/>';
}

输出为:

set1 T
set1 h
set1 e
set1
set1 q
set1 u
set1 i
set1 c
set1 k
set1
set1 b
set1 r
set1 o
set1 w
set1 n
set2
set2 f
set2 o
set2 x
set2
set2 j
set2 u
set2 m
set2 p
set2 s
set2
set2 o
set2 v
set2 e
set2 r
set3
set3 t
set3 h
set3 e
set3
set3 l
set3 a
set3 z
set3 y
set3
set3 d
set3 o
set3 g
set3 .

因此,我已经将每个单词/字母正确地分组为3组/集合。现在,当我换到一个新的集合/组时,我陷入了困境。我需要确定一个组第一次更改的时间。所以在这种情况下,我需要知道集合2以";f";这是";狐狸";。然后第三组以";t〃;它是"的第一个字;";。

我需要知道这一点,因为当组/集的第一次出现更改时,我必须执行一个附加条件。希望你能帮忙?

谢谢!

有一种更好的方法,更具动态性,因为您只需要3个集合。

制作一个数组,那么任何集合的第一个字符都是[0][0],例如:

<?php
$word = 'The quick brown fox jumps over the lazy dog.';
$sets = array_chunk(explode(' ',$word), 3);
$sets = array_combine(array_map(function($key){ return 'set'.($key+1); }, array_keys($sets)), $sets);
/*
Array
(
[set1] => Array
(
[0] => The
[1] => quick
[2] => brown
)
[set2] => Array
(
[0] => fox
[1] => jumps
[2] => over
)
[set3] => Array
(
[0] => the
[1] => lazy
[2] => dog.
)
)
*/
// then to grab first chars
// f
echo $sets['set2'][0][0];
// t
echo $sets['set3'][0][0];

https://3v4l.org/TOFbo

要获得总集合,您只需要count($sets),要获得一个集合,您仅需要$set['set1'],要获得所有集合中的所有第一个单词,您只需array_column($sets, '0'),要获得全部密钥/集合array_keys($sets)。比循环遍历每个字符和计数要简单得多。

然后/或将其重新组合,在其上重新循环:

$str = '';
foreach ($sets as $set) $str .= implode(' ', $set).' ';
echo trim($str);

或使用reduce:

echo trim(array_reduce($sets, function($acc, $set) {
return $acc .= implode(' ', $set).' ';
}, ''));

如果您希望保留现有的逻辑规则集(例如,如果您有进一步的扩展计划(,您可以创建一个数组并将每个字符保存为特定的";设置";用一个键(例如;set1";。然后你可以从任何一个集合中获得任何字符,如下所示:

$sets[$set_key][$char_pos];
// e.g.
echo $sets["set1"][0];
echo $sets["set2"][0];
echo $sets["set3"][0];

仅供参考,这模仿了您当前的结果,即在set1之后的所有集合都以空格开头,因为它使用了您现有的规则。(如果这不是你打算做的,你可能需要调整你的规则(

您的改编代码:

$word = 'The quick brown fox jumps over the lazy dog.';
$word_length = strlen($word);
$space_count = 1;
$sets = array();
for ($i=0;$i<=$word_length-1;$i++) {

if ($word[$i] == ' ')
{
$space_count++;
}
if ($space_count <= 3) //1st set
{
$sets['set1'][] = $word[$i];
}
elseif ($space_count > 3 && $space_count <= 6) //2nd set
{
$sets['set2'][] = $word[$i];
}
elseif ($space_count > 6 && $space_count <= 9) //3rd set
{
$sets['set3'][] = $word[$i];
}
elseif ($space_count > 9) //4th set
{
$sets['set4'][] = $word[$i];
}
}
print_r($sets);

输出:

Array
(
[set1] => Array
(
[0] => T
[1] => h
[2] => e
[3] =>  
[4] => q
[5] => u
[6] => i
[7] => c
[8] => k
[9] =>  
[10] => b
[11] => r
[12] => o
[13] => w
[14] => n
)
[set2] => Array
(
[0] =>  
[1] => f
[2] => o
[3] => x
[4] =>  
[5] => j
[6] => u
[7] => m
[8] => p
[9] => s
[10] =>  
[11] => o
[12] => v
[13] => e
[14] => r
)
[set3] => Array
(
[0] =>  
[1] => t
[2] => h
[3] => e
[4] =>  
[5] => l
[6] => a
[7] => z
[8] => y
[9] =>  
[10] => d
[11] => o
[12] => g
[13] => .
)
)

最好的答案显然是劳伦斯的答案。它优雅而高性能(与for循环或任何你可以自己编码的东西相比,PHP的数组函数都非常高性能(

但是,如果你对这些功能不满意,基本上要回答你的问题并保持你的逻辑。我建议使用模测试:

$word = 'The quick brown fox jumps over the lazy dog.';
$word_length = strlen($word);
$space_count = 0;
$j = 1;
$check_spacing = false;
for ($i=0;$i<=$word_length-1;$i++)
{
if ($word[$i] == ' ')
{
$space_count++;
// we have a space, so we need to test if it's the third or not
$check_spacing = true;
continue;
}
// Enter in it every 3 spaces or if it's the first char of your sentence.
if ($i == 0 || ($space_count % 3 == 0 && $check_spacing))
{
// So we'll not test the condition before the next space character
$check_spacing = false;
echo sprintf('The first letter of set %s is %s <br>', $j++, $word[$i]);
}
}

有了这个,你应该有这样的东西,它适用于每一个句子长度:

集合1的第一个字母是T

集合2的第一个字母是f

集合3的第一个字母是t

最新更新