Php 如何在杂乱的字母中查找字符串

Php 如何在杂乱的字母中查找字符串,php,Php,我需要编写一个程序来在混乱的字母中查找一个单词。例如:考虑字符串$example=“ahwerlyp”;我需要从字符串中找到“帮助”这个词。我怎样才能找到它。任何帮助都将不胜感激 我尝试使用substr()函数,但它只返回字符串。如果它都在同一行中,否则它返回零 <?php $example = "ahwerlyp"; $findword = "help"; /**how to find the word help from it**/ if($findword is present

我需要编写一个程序来在混乱的字母中查找一个单词。例如:考虑字符串$example=“ahwerlyp”;我需要从字符串中找到“帮助”这个词。我怎样才能找到它。任何帮助都将不胜感激

我尝试使用substr()函数,但它只返回字符串。如果它都在同一行中,否则它返回零

<?php
$example = "ahwerlyp";
$findword = "help";
/**how to find the word help from it**/


 if($findword is present in $example)
{
echo "exists";
}

?>

  • 将每个单词拆分为包含所有单个字母的数组。(例如,可以使用
    preg\u split(“//u”、“word”、-1,preg\u split\u NO\u EMPTY)

  • 对于每个单词,计算每个字母出现的次数-
    array\u count\u values
    提供一个数组,其中输入数组值(我们的单个字母)作为键,计数作为值

  • 循环第二个单词中已计数的字母,并检查第一个单词中相同字母的计数是否至少大于或等于。如果第二个单词中的任何字母都不是这样,那么第一个单词中就没有“包含”

  • 让我们将所有这些都封装到一个漂亮的小函数中,我们得到以下结果:

    function does_a_contain_b($a, $b) {
      // split both words into individual letters, and count their occurrences
      $letters_given  = array_count_values(preg_split('//u', $a, -1, PREG_SPLIT_NO_EMPTY));
      $letters_needed = array_count_values(preg_split('//u', $b, -1, PREG_SPLIT_NO_EMPTY));
    
      // we assume b is going to be contained in a for now
      $contained = true;
    
      foreach($letters_needed as $letter => $count) {
        // if the letter from 2nd word does not occur in the 1st one at all,
        // or the count in 2nd is not at least equal to that of 1st,
        // we set our flag to false, and break out of the loop
        if(!isset($letters_given[$letter]) || $letters_given[$letter] < $count) {
          $contained = false;
          break;
        }
      }
      return $contained;
    }
    
    // a couple of test cases
    var_dump(
      does_a_contain_b('ahwerlyp', 'help'),  // true
      does_a_contain_b('ahwerlyp', 'hhelp'), // false
      does_a_contain_b('ahwerlyp', 'hype'),  // true
      does_a_contain_b('ahwerlyp', 'foobar') // false
    );
    
    函数a\u是否包含b($a,$b){
    //将两个单词拆分为单独的字母,并计算它们出现的次数
    $letters_given=数组_count_值(preg_split('//u',$a,-1,preg_split_NO_EMPTY));
    $letters_needed=数组_count_值(preg_split('//u',$b,-1,preg_split_NO_EMPTY));
    //我们假设现在b将包含在a中
    $contained=true;
    foreach($letter=>$count所需的字母){
    //如果第二个单词的字母根本不出现在第一个单词中,
    //或者第二个的计数至少不等于第一个的计数,
    //我们将标志设置为false,然后跳出循环
    如果(!isset($letters_gived[$letter])|$$letters_gived[$letter]<$count){
    $contained=假;
    打破
    }
    }
    返回所含的美元;
    }
    //几个测试用例
    瓦鲁垃圾场(
    _a_是否包含_b('ahwerlyp','help'),//true
    _a_是否包含_b('ahwerlyp','hhelp'),//false
    _a_是否包含_b('ahwerlyp','hype'),//true
    _a_是否包含_b('ahwerlyp','foobar')//false
    );
    
    请用以下代码替换您的代码:

    <?php
    $example = "ahwerlyp";
    $findword = "help";
    
    if (!array_diff(str_split($findword), str_split($example))) {
           echo "exists";
    } else {
           echo "not exist";
    }
    ?>
    
    
    
    数一数字母就行了。如果它们都存在并且足够(按计数)使用
    $findword
    ,那么答案是
    true
    ,否则是
    false
    。或者你的意思是子序列(意思是字母的顺序很重要)?@vivek_23我只需要检查字符串中是否存在单词help。我怎么做?@vivek_23你能显示一些代码吗?好的。
    $example=“pehawryl”的输出是什么$findword=“帮助”?另外,你的问题太宽泛了。你能表现出你的努力吗?