如何访问数组的元素并计算其中的单词?

时间:2018-08-29 10:50:28

标签: php arrays printing echo element

所以,我进行了一次练习,我做了一些研究,并设法用点把preg_split分割成字符串。现在,我已经有了一个我想要的数组,而且我想进入这个数组的元素内,这样我就可以计算每个元素中的单词。 我可以帮忙吗? $ test字符串为希腊语。

$test = "Αυτή είναι η 1η δοκιμασία. Πρέπει να την ολοκληρώσω. Ώστε να μου δώσουν την 2η δοκιμασία. Και τέλος, την 3η δοκιμασία." ;
$res = preg_split ("/(.*?\.*?)\../", $test, NULL,
        PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($res);

结果是这样的:

Array
(
    [0] => Αυτή είναι η 1η δοκιμασία
    [1] => Πρέπει να την ολοκληρώσω
    [2] => Ώστε να μου δώσουν την 2η δοκιμασία
    [3] => Και τέλος, την 3η δοκιμασία.
)

就像我之前说的,我想访问每个元素(例如[0],[1],[2],[3]),并打印每个元素具有的单词数。但是我找不到方法...

4 个答案:

答案 0 :(得分:0)

这应该有效-

foreach($res as $str) {
   print_r(count(explode(' ', $str)));
}

输出

5
4
7
5

Demo

答案 1 :(得分:0)

您可以将preg_split与array_map一起使用。

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"> 
</script>

输出:https://3v4l.org/JNBYX

答案 2 :(得分:0)

您可以在遍历数组时使用word_wount来了解单词的数量,并通过substr来了解单词中是否包含单词。

$test = "Αυτή είναι η 1η δοκιμασία. Πρέπει να την ολοκληρώσω. Ώστε να μου δώσουν την 2η δοκιμασία. Και τέλος, την 3η δοκιμασία." ;
$res = preg_split(
    "/(.*?\.*?)\../",
    $test,
    null,
        PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
var_dump($res);

$wordcounts = array_map(function ($item) {
    return count(preg_split('/\s+/', $item));
}, $res);

var_dump($wordcounts);

上面的示例将输出:

<?php

$str = "Hello fri3nd, you're
       looking          good today!";

print_r(str_word_count($str, 1));
print_r(str_word_count($str, 2));
print_r(str_word_count($str, 1, 'àáãç3'));

echo str_word_count($str);

?>

来源:PHP.net

一种更强大有效的方法是

Array
(
    [0] => Hello
    [1] => fri
    [2] => nd
    [3] => you're
    [4] => looking
    [5] => good
    [6] => today
)

Array
(
    [0] => Hello
    [6] => fri
    [10] => nd
    [14] => you're
    [29] => looking
    [46] => good
    [51] => today
)

Array
(
    [0] => Hello
    [1] => fri3nd
    [2] => you're
    [3] => looking
    [4] => good
    [5] => today
)


<?php
$text = 'This is a test';
echo strlen($text); // 14

echo substr_count($text, 'is'); // 2

// the string is reduced to 's is a test', so it prints 1
echo substr_count($text, 'is', 3);

// the text is reduced to 's i', so it prints 0
echo substr_count($text, 'is', 3, 3);

// generates a warning because 5+10 > 14
echo substr_count($text, 'is', 5, 10);


// prints only 1, because it doesn't count overlapped substrings
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
?>

真的取决于您的代码结构和输入内容

答案 3 :(得分:0)

您可以按照所需的方式遍历数组:使用forforeach循环。

$sentencesCount = count($res);
$wordsCounts = [];
for($i = 0; $i < $sentencesCount; $i++) {
    $wordsCounts[$i] = str_word_count($res[$i]);
}

print_r($wordsCounts);

OR

$wordsCounts = [];
foreach($res as $key => $words) {
    $wordsCounts[$key] = str_word_count($words);
}

print_r($wordsCounts);

另外,请检查is PHP str_word_count() multibyte safe?和文档http://php.net/manual/en/function.str-word-count.php