PHP搜索字符串(带通配符)

编程入门 行业动态 更新时间:2024-10-26 05:22:57
本文介绍了PHP搜索字符串(带通配符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以将通配符放在字符串中?我问的原因是因为当前我有一个功能可以搜索两个子字符串之间的子字符串(即在我的狗有跳蚤"句子中抓取"my"和"has fleas"之间的内容,导致"dog" ).

Is there a way to put a wildcard in a string? The reason why I am asking is because currently I have a function to search for a substring between two substrings (i.e grab the contents between "my" and "has fleas" in the sentence "my dog has fleas", resulting in "dog").

function get_string_between($string, $start, $end){ $string = " ".$string; $ini = strpos($string,$start); if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; return substr($string,$ini,$len); }

我想做的是在字符串中使用通配符进行搜索.所以说我在句子我的狗有跳蚤"中的%WILDCARD%"和有跳蚤"之间搜索-它仍然会输出"dog".

What I want to do is have it search with a wildcard in the string. So say I search between "%WILDCARD%" and "has fleas" in the sentence "My dog has fleas" - it would still output "dog".

我不知道我是否解释得太好,但希望有人会理解我的:P.非常感谢您的阅读!

I don't know if I explained it too well but hopefully someone will understand me :P. Thank you very much for reading!

推荐答案

这是正则表达式实际上有用的少数情况之一. :)

This is one of the few cases where regular expressions are actually helpful. :)

if (preg_match('/my (\w+) has/', $str, $matches)) { echo $matches[1]; }

请参阅 preg_match 的文档.

更多推荐

PHP搜索字符串(带通配符)

本文发布于:2023-10-24 14:57:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   字符串   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!