如何使用php计算非英语句子中的单词数?

如何使用php计算非英语句子中的单词数?,php,Php,我想用PHP计算非英语句子中的单词数。为此,我尝试了str_word_count,但它没有给我想要的结果,我不想使用mb_strlen,因为它给了我字符串的长度。所以如果有人能帮我,请 到目前为止,我已经做到了 function count_words($string) { $string = html_entity_decode($string); $string= str_replace("'", "'", $string);

我想用PHP计算非英语句子中的单词数。为此,我尝试了str_word_count,但它没有给我想要的结果,我不想使用mb_strlen,因为它给了我字符串的长度。所以如果有人能帮我,请

到目前为止,我已经做到了

function count_words($string) {
    $string = html_entity_decode($string);
    $string= str_replace("'", "'", $string);
    $t= array(' ', "t", '=', '+', '-', '*', '/', '', ',', '.', ';', ':', '[', ']', '{', '}', '(', ')', '<', '>', '&', '%', '$', '@', '#', '^', '!', '?', '~'); // separators
    $string= str_replace($t, " ", $string);
    $string= trim(preg_replace("/s+/", " ", $string));
    $num= 0;
    if (my_strlen($string)>0) {
        $word_array= explode(" ", $string);
        $num= count($word_array);
    }
    return $num;
}


它需要给我5的输出,但给6,我发现问题发生在我使用任何逗号或倒逗号时,所以我如何纠正它,我只想显示其中的3个单词。怎么可能呢。

您应该像这样使用正则表达式:

<?php
$string = "আমি 'আমার' দেশ, ভারতকে ভালবাসি";
$pattern = '/[^\x00-\x7F]+/';
echo preg_match_all($pattern, $string);
?>

编辑 回答你的全部问题:

<?php
$string = "আমি 'আমার' দেশ, ভারতকে ভালবাসি";
$pattern = '/[^\x00-\x7F]+/';
$words = preg_match_all($pattern, $string, $res);
echo $res[0][0] . " " . $res[0][1] . " " . $res[0][2]
?>

您的数组
$t
包含一些用于在原始字符串中创建额外白间距的符号。 由于这是您在explode中使用的字符(
'
),因此数组
$word\u数组
将为每个额外的空白包含空字符串

为了去除那些最肯定不是单词的空字符串,您可以像现在一样简单地在最后过滤数组

最后,如果要处理字符串中的单词,函数可以返回单词数组。然后可以从数组中取出3个或多少个单词

$string = "আমি 'আমার' দেশ, ভারতকে ভালবাসি";

function words($string)
{
    $string = html_entity_decode($string);
    $string = str_replace("'", "'", $string);
    $t = array(' ', "t", '=', '+', '-', '*', '/', '', ',', '.', ';', ':', '[', ']', '{', '}', '(', ')', '<', '>', '&', '%', '$', '@', '#', '^', '!', '?', '~'); // separators
    $string = str_replace($t, " ", $string);
    $string = trim(preg_replace("/s+/", " ", $string));

    $word_array = [];
    if (my_strlen($string) > 0) {
        $word_array = explode(" ", $string);

        // Filter out those redundant empty strings that might be a by-product
        // of replacing characters from $t with a whitespace ' ' and explode.
        $word_array = array_filter($word_array, function ($word) {
            return $word !== '';
        });
        // PHP 7.4
        // $word_array = array_filter($word_array, fn ($word) => $word !== '');
    }

    return $word_array;
}

$words = words($string);
echo count($words) . PHP_EOL;

// Additionally you could output the first 3 words
echo implode(' ', array_slice($words, 0, 3));

当然,在计算字数时,您的原始功能是否可靠,您必须仔细检查自己。

以及echo 3charcters@BabyBabai答案更新否假设我只想要
আমি 'আমার' দেশ到此为止,我如何回应它。@Babybai您应该使用您的正则表达式模式,例如,在这种情况下,您的模式应该是:([^\x00-\x7F]|')+好的,我正在尝试,您返回6的原因是因为单词
দেশ,$word\u数组中过滤空字符串
@Remy yes你能告诉我怎么做吗这回答了你的问题吗@我发现了这个error@BabyBabai已经害怕了,更新了旧的关闭。
$string = "আমি 'আমার' দেশ, ভারতকে ভালবাসি";

function words($string)
{
    $string = html_entity_decode($string);
    $string = str_replace("'", "'", $string);
    $t = array(' ', "t", '=', '+', '-', '*', '/', '', ',', '.', ';', ':', '[', ']', '{', '}', '(', ')', '<', '>', '&', '%', '$', '@', '#', '^', '!', '?', '~'); // separators
    $string = str_replace($t, " ", $string);
    $string = trim(preg_replace("/s+/", " ", $string));

    $word_array = [];
    if (my_strlen($string) > 0) {
        $word_array = explode(" ", $string);

        // Filter out those redundant empty strings that might be a by-product
        // of replacing characters from $t with a whitespace ' ' and explode.
        $word_array = array_filter($word_array, function ($word) {
            return $word !== '';
        });
        // PHP 7.4
        // $word_array = array_filter($word_array, fn ($word) => $word !== '');
    }

    return $word_array;
}

$words = words($string);
echo count($words) . PHP_EOL;

// Additionally you could output the first 3 words
echo implode(' ', array_slice($words, 0, 3));
5
আমি 'আমার' দেশ