douba3943 2013-10-01 09:08
浏览 44

在PHP中搜索非空白正确的字符串

I'm have a project where I need to find occurrences of a string in a large body of text. The search string is known to be present in the larger text, however for reasons beyond my control they are not white-space correct, in that they are missing spaces between some of the words.

For example I the string to find is (not the lack of space between brown and fox:

quick brownfox jumps

And I need to find this in:

The quick brown fox jumps over the lazy dog.

I need to be able to modify the haystack to wrap the found terms with an identifying tag so I'll end up with something like:

The <span class="found">quick brown fox jumps</span> over the lazy dog.

I've looked into using regex in free-spacing mode which seems to not quite do what I need, I considered stripping all white space from the search terms and adding \s* between each character but thought this might have a horrendous effect on performance (can any regex experts could confirm or deny that?).

Are there any possible non-regex solutions to look into.

Thanks

  • 写回答

2条回答 默认 最新

  • doulan4371 2013-10-01 09:15
    关注

    The best way in this case would be to remove all the whitespaces in the search string, and the target string. And then check if the string is present or not:

    $haystack = 'The quick brown fox jumps over the lazy dog.';
    $needle = 'quick brownfox jumps';
    
    $haystack = preg_replace("\s+", "", $haystack);
    $needle = preg_replace("\s+", "", $needle);
    
    if (strpos($haystack, $needle) !== false) {
        echo 'true';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: