加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 从正则表达式中获取不区分大小写的单词

发布时间:2021-08-28 03:46:45 所属栏目:大数据 来源: https://www.jb51.cc
导读:假设,我有一个字符串 $res = "there are many restaurants in the city. Restaurants like xyz,abc. one restaurant like....."; 在上面的例子中,我们可以在3个地方找到餐厅.我需要数到3. $pattern = '/Restaurant/';preg_match($pattern, substr($res,10),

假设,我有一个字符串

$res = "there are many restaurants in the city. Restaurants like xyz,abc. one restaurant like.....";

在上面的例子中,我们可以在3个地方找到餐厅.我需要数到3.

$pattern = '/Restaurant/';
preg_match($pattern, substr($res,10), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

还有一个问题
这与上述问题有关.也就是说,我有像Food&amp ;;这样的文字.饮料.我需要将这个词与食物或饮料或海鲜相匹配……等等,任何人都可以帮我解决这个问题.提前致谢.

解决方法:

你可以使用这样的正则表达式:

$pattern = '/restaurants?/i';

我对原始正则表达式进行了两处更改:

>添加i修饰符 – 这是不区分大小写的标志.
>添加s?到搜索字符串的末尾.这使得最后一个s字符可选.它匹配s的零或一个出现.

请注意,因为我们使用不区分大小写的标志,所以此正则表达式还将匹配以下内容:

> ResTaurants
> rEstaurantS
> RESTauRANTS

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读