简体   繁体   English

PHP 如何计算可以从字符串中的字母组成单词的次数?

[英]PHP How to count the number of times you can make a word from letters in string?

I wanted to make a function which you can see how many times you can make a random word from a text.我想制作一个函数,您可以查看可以从文本中随机生成多少次单词。

For example:例如:

The text is: Ham followed now ecstatic use speaking exercise may repeated.文字是:火腿跟着现在狂喜使用口语练习可重复。 Himself he evident oh greatly my on inhabit general concern.他本人很明显哦,我对居住普遍的关注。 It allowance prevailed enjoyment in it.它允许在其中占优势。 Calling observe for who pressed raising his.呼叫观察谁按下了抬起他的。 Can connection instrument astonished unaffected his motionless preference.可连仪惊讶的不影响他一动不动的喜好。 Announcing say boy precaution unaffected difficulty alteration him.宣布说男孩预防不受影响的困难改变了他。

I want to know how many times you can write the word "potato".我想知道你可以写多少次“土豆”这个词。

so, I started to make a function (which is not good enough of course):所以,我开始做一个函数(当然这还不够好):

<?php
$text = "Ham followed now ecstatic use speaking exercise may repeated.
Himself he evident oh greatly my on inhabit general concern. It allowance 
prevailed enjoyment in it. Calling observe for who pressed raising his. Can 
connection instrument astonished unaffected his motionless preference. 
Announcing say boy precaution unaffected difficulty alteration him.";
$word = "Potato";

//function
    function count($text) {
 for ($i = 0; $i <= strlen($text); $i++) {
  if ($text[$i] == "p") {
$p = 0;
$p++;
echo $p; } } }
?>

My question now is: how do you count from now on the number of times you can make the word "potato"?我现在的问题是:从现在开始,你如何计算“土豆”这个词的次数?

Thanks for replying.感谢回复。

此函数可帮助您获取paroto在该字符串中使用的次数

echo substr_count($text, $word);

My approach would be to go through the text storing each character in a dictionary where the key is the letter and the value is the number of occurrences in the text.我的方法是遍历存储在字典中的每个字符的文本,其中键是字母,值是文本中出现的次数。 Once you finish processing the text you can calculate how many times you can spell potato处理完文本后,您可以计算出可以拼写多少次马铃薯

Firstly, you cannot declare count function as it is already declared.首先,您不能声明count函数,因为它已经声明了。

Try this:尝试这个:

function countt($text, $word, $count = 0) {
    $found = '';
    for($i=0; $i<strlen($word);$i++){
        for($j=0; $j<strlen($text);$j++){
            if($word{$i} === $text{$j}){
                $text = substr($text,0,$j).substr($text,$j+1);
                $found.= $word{$i};
                if($found === $word){
                    $count++;
                    return countt($text,$word,$count);
                }
                break;
            }
        }
    }
    return $count;
}

echo countt($text,'potato');//6

Explanation: function finds occurences of each letter of a word in text and removes it from the text.说明:函数查找文本中单词的每个字母的出现次数并将其从文本中删除。 Once it completes new word that is identical to the searched one, it calls this function again with new text, that misses those used letters, until there is no more letters left that could complete another word.一旦它完成与搜索到的相同的新单词,它会再次使用新文本调用此函数,新文本会遗漏那些使用过的字母,直到没有更多的字母可以完成另一个单词。

You can play with it here:你可以在这里玩它:

http://sandbox.onlinephpfunctions.com/code/ba579be33a82a6abdc0fc285cc4a631186eb7b29 http://sandbox.onlinephpfunctions.com/code/ba579be33a82a6abdc0fc285cc4a631186eb7b29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM