搜索部分字符串 PHP [英] Searching partial strings PHP

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

问题描述

如何在键入(不是使用 MySQL)时像 MySQL 中的 LIKE 函数那样搜索部分字符串,但在搜索字符串时使用 PHP,例如

How can you search a partial string when typing (not to use MySQL) like the LIKE function in MySQL but using PHP when searching a string, e.g.

<?php    

$string = "Stackoverflow";

$find = "overfl";

if($find == $string)
{
    return true;
}
else
{
    return false
}

?>

但这显然行不通,但是有没有可以搜索部分字符串的功能?那会很棒:)

But that will obviously work won't, but is there a function where you can search partially of a string? That would be great :)

如果它在一个数组中呢?

What if it was in an array?

如果我使用 strpos,它会产生回声,如果我使用它,它就像 truetruetruetruetrue.

if i use the strpos, it does the echo, If I use it, it goes like truetruetruetruetrue.

推荐答案

我倾向于使用 strpos

$needle='appy';
$haystack='I\'m feeling flappy, and you?';

if(strpos($haystack,$needle)!==false){
   //then it was found
   }

如果您希望它忽略大小写,请使用 stripos.

If you want it to ignore case, use stripos.

请注意,这其中的一个微妙之处在于,如果指针位于大海捞针的最开始,在位置 0,则返回整数 0.这意味着您必须使用严格比较与 false 进行比较,否则会产生假阴性.

Note that a subtlety about this is that if the needle is at the very start of the haystack, in position 0, integer 0 is returned. This means you must compare to false, using strict comparison, or it can produce a false negative.

如手册中所述,上面链接

As noted in the manual, linked above

警告

此函数可能返回布尔值FALSE,但也可能返回一个计算结果为的非布尔值FALSE,例如 0 或 "".请阅读有关更多信息的布尔值部分信息.使用 === 运算符测试 this 的返回值功能.

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

就使用数组而言,strpos 意味着采用两个字符串.使用数组将产生警告:strpos() 期望参数 1 为字符串,给定数组 或 1警告:strpos():needle is not a string or an integer`.

As far as using arrays, strpos is meant to take two strings. Using an array will produce Warning: strpos() expects parameter 1 to be string, array given or 1Warning: strpos(): needle is not a string or an integer`.

好的,假设您有一个要搜索的字符串数组.

Okay, let's say you have an array of strings for which to search.

你可以

$needles=array('hose','fribb','pancake');
$haystack='Where are those pancakes??';

foreach($needles as $ndl){
   if(strpos($haystack,$ndl)!==false){ echo "'$ndl': found<br>\n"; }
   else{ echo "'$ndl' : not found<br>\n"; }
}

另一种不使用数组在一个字符串中搜索多个字符串的方法...这只告诉您是否至少找到了一个匹配项.

Another way of searching for multiple strings in one string, without using an array... This only tells you whether at least one match was found.

$haystack='Where are those pancakes??';
$match=preg_match('#(hose|fribb|pancake)#',$haystack);
//$match is now int(1)

或者,使用 preg_match_all 查看总共有多少匹配项.

Or, use preg_match_all to see how many matches there are, total.

$all_matches=preg_match_all('#(hose|fribb|pancake)#',$haystack,$results);
//all_matches is int(2). Note you also have $results, which stores which needles matched.

在这里,搜索词是一个正则表达式.() 将术语组合在一起,| 表示或".# 表示模式的开始和结束.正则表达式很快就会变得非常复杂,但当然,它们可以工作!出于性能原因,它们通常会被避免,但是如果您正在测试多个字符串,这可能比上面描述的数组循环方法更有效.我相信还有其他方法可以做到这一点.

In that, the search term is a regular expression. () groups the terms together, and | means 'or'. # denotes the beginning and end of the pattern. Regexes can get pretty complicated quickly, but of course, they work! They are often avoided for performance reasons, but if you're testing multiple strings, this might be more efficient than they array looping method described above. I'm sure there are also other ways to do this.

这篇关于搜索部分字符串 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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