在字符串中查找确切的字符串 [英] Find exact string inside a string

查看:87
本文介绍了在字符串中查找确切的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串"Mures"和"Maramures".我该如何构建一个搜索功能,当有人搜索Mures时,它只会向他返回包含"Mures"一词的帖子,而不返回包含"Maramures"一词的帖子.到目前为止,我一直尝试使用strstr,但是现在可以使用了.

I have two strings "Mures" and "Maramures". How can I build a search function that when someone searches for Mures it will return him only the posts that contain the "Mures" word and not the one that contain the "Maramures" word. I tried strstr until now but it does now work.

推荐答案

您可以使用正则表达式,并用\b单词边界将单词包围起来

You can do this with regex, and surrounding the word with \b word boundary

preg_match("~\bMures\b~",$string)

示例:

$string = 'Maramures';
if ( preg_match("~\bMures\b~",$string) )
  echo "matched";
else
  echo "no match";

这篇关于在字符串中查找确切的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆