dongmei9961 2015-07-10 13:13
浏览 143
已采纳

查找字符串中与多个数组键匹配的所有单词

I have an array like array("red","blue","azure"...) and i have a string that might contain some of the words. The task is to get array of all the matching colors from the given string.

String example: "Red fox met a blue whale". It should output ["red","blue"]

Give me a starting point to go on with.

Thanks, Martti

  • 写回答

5条回答 默认 最新

  • douhuai2015 2015-07-10 13:24
    关注

    str_word_count() with a format argument of 1 or 2, then an array_intersect().... but watch out for case-sensitivity, force it all to lower case first

    $matchWords = array("red","blue","azure");
    $sentence = "Red fox met a blue whale";
    
    $result = array_intersect(
        $matchWords,
        str_word_count(strtolower($sentence), 1)
    );
    
    var_dump($result);
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效