对数组运行查询
我想使用 PHP 对数组或字符串运行查询
假设我有一个字符串:
$Iamastring = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc velit est, porta sed feugiat vitae, sodales et nisl. Suspendisse ut."
我想对该字符串运行查询来找到它:
"Lorem ipsum" OR ("dolor" AND "sodales")
在这种情况下,这是真的:)。最好的方法是什么?
I want to run a query over an Array or string with PHP
Let say I got a string:
$Iamastring = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc velit est, porta sed feugiat vitae, sodales et nisl. Suspendisse ut."
And I want to run a query over that string to find it:
"Lorem ipsum" OR ("dolor" AND "sodales")
In this case it would be true :). How is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正则表达式:)
preg_match('\(Lorem ipsum)|(dolor.+sodales)\', $matches)
。或者不同正则表达式的变体regexp :)
preg_match('\(Lorem ipsum)|(dolor.+sodales)\', $matches)
. Or variations with different regexp'sRegExp 是一种方法,但如果您想使用内置字符串函数:
请注意,PHP 内置字符串函数比 RegExp 更快,但每种方法的适用性因上下文而异。
RegExp is one way, though if you want to use inbuilt string functions:
Note that PHPs inbuilt string functions are faster than RegExps, but each approach varies in appropriateness depending on context.
您可以使用正则表达式来完成此操作。查看类似 preg_match() 的内容,
如果满足以下条件,则应返回 true 或 false模式匹配:
You would do this with a regular expression. Check out something like preg_match()
The following should return true or false if the pattern matches: