doukang2003 2016-06-07 23:03
浏览 102
已采纳

PHP字符串不敏感的字数在2个字符串之间共同

I'm trying to take 2 pieces of text in php like this...

"A cat jumped over the hat"

"The mad hatter jumped over his cat"

And get results like this...

the
cat
jumped
over 

(i.e. the common words between the strings, where hat is NOT included because it's part of another word in the second string)

I've found a bunch of examples to help count occurrences of 1 string within another, but that would end up giving me the "hatter" problem so I'm guessing I need to tokenize both strings into word-lists and do one-to-one compares somehow.

Struggling to visualise an efficient way to achieve that though so appreciate any thoughts at all on what the correct approach is. Thanks!

  • 写回答

2条回答 默认 最新

  • douxi8119 2016-06-07 23:10
    关注

    For this problem, I'd use explode to separate each string into words, then create an array for each string where the keys are the words, and the values all just true. Then, you can take one of the arrays, loop through its keys, and check whether they're present in the other array.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题