1

So, i have an exercise , i did some research and managed to preg_split my string by dots. Now i've got an Array the way i want it to be and i want to get inside the elements of this Array so i can count the words in each one of the elements. Can i have some help with this one? The $test string is in Greek.

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

the result is something like this:

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

and as i said earlier i want to access every element (e.g [0], [1], [2], [3]) and print the number of words each one of them has. But i can not find the way how...

Nikolaos
  • 11
  • 3
  • If you want to access each element in array, use foreach loop, like that: foreach( $res as $item ) { // $item is available }. To count words, you may split your $item by space. It's not perfect solution but for beginners example this should do. – Łukasz Aug 29 '18 at 10:52
  • 2
    Use [foreach](https://secure.php.net/manual/en/control-structures.foreach.php) and [str_word_count()](https://secure.php.net/manual/en/function.str-word-count.php) which will ... "_return information about words used in a string_" – brombeer Aug 29 '18 at 10:57
  • str_word_count counts all occurence. It doesn't look for a specific one – Dice Aug 29 '18 at 11:12
  • @Dice True. Looking for a specific word is not required though – brombeer Aug 29 '18 at 11:16
  • You got the answer then mate :) – Dice Aug 29 '18 at 11:17
  • Thought he was looking for a needle – Dice Aug 29 '18 at 11:17

4 Answers4

0

This should work -

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

Output

5
4
7
5

Demo

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

You can use preg_split with array_map.

$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);

Output: https://3v4l.org/JNBYX

Ketan Yekale
  • 2,108
  • 3
  • 26
  • 33
0

You can use word_wount when going through the array to know the amount of words, and substr to know if a word is in the sentence.

<?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);

?>

The above example will output:

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');
?>

Source : PHP.net

A more powerful and efficient way to do it would be to

<?php
      if(preg_match($pattern,$sentence)); /** Sentece can be a list like $sentence[0]*/
      $count++; 
?>
the word.

Really depends on your code structure and the input

Dice
  • 236
  • 1
  • 8
0

You can iterate over your array in the way you want: using for or foreach loops.

$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);

Also, please check is PHP str_word_count() multibyte safe? and docs http://php.net/manual/en/function.str-word-count.php

iwex
  • 384
  • 3
  • 17