对数组运行查询

发布于 2024-10-03 23:03:10 字数 343 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[浮城] 2024-10-10 23:03:10

正则表达式:) preg_match('\(Lorem ipsum)|(dolor.+sodales)\', $matches)。或者不同正则表达式的变体

regexp :) preg_match('\(Lorem ipsum)|(dolor.+sodales)\', $matches). Or variations with different regexp's

他夏了夏天 2024-10-10 23:03:10

RegExp 是一种方法,但如果您想使用内置字符串函数:

$haystack="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc velit est, porta sed feugiat vitae, sodales et nisl. Suspendisse ut.";

if((strrpos($haystack, "Lorem Ipsum")!==false)||((strrpos($haystack, "dolor")!==false)&&(strrpos($haystack, "sales")!==false))){

// do this if true

}

请注意,PHP 内置字符串函数比 RegExp 更快,但每种方法的适用性因上下文而异。

RegExp is one way, though if you want to use inbuilt string functions:

$haystack="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc velit est, porta sed feugiat vitae, sodales et nisl. Suspendisse ut.";

if((strrpos($haystack, "Lorem Ipsum")!==false)||((strrpos($haystack, "dolor")!==false)&&(strrpos($haystack, "sales")!==false))){

// do this if true

}

Note that PHPs inbuilt string functions are faster than RegExps, but each approach varies in appropriateness depending on context.

锦欢 2024-10-10 23:03:10

您可以使用正则表达式来完成此操作。查看类似 preg_match() 的内容,

如果满足以下条件,则应返回 true 或 false模式匹配:

preg_match ('/(Lorem ipsum)|(dolor+sodales)/' $Iamastring);

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:

preg_match ('/(Lorem ipsum)|(dolor+sodales)/' $Iamastring);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文