关于PHP中的strpos的问题 [英] Question about strpos in PHP

查看:82
本文介绍了关于PHP中的strpos的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想弄清楚...

I'm just trying to figure that out...

$mystring = "/abc/def/hij";
$find = "/abc";

echo(strpos($mystring, $find) . "<br>");
if (strpos($mystring, $find) >= 0) {
    echo("found");
} else {
    echo("not found");
}

这将给出: 0 找到

$mystring = "/abc/def/hij";
$find = "/fffff";

echo(strpos($mystring, $find) . "<br>");
if (strpos($mystring, $find) >= 0) {
    echo("found");
} else {
    echo("not found");
}

输出: [空白的] 找到

output : [blank] found

现在,如果我更改比较器并使用!= False"而不是> = 0"

Now if I change the comparator and use "!= False" instead of ">= 0"

$mystring = "/abc/def/hij";
$find = "/fffff";

echo(strpos($mystring, $find) . "<br>");
if (strpos($mystring, $find) **!= false**) {
    echo("found");
} else {
    echo("not found");
}

这几乎在所有情况下都有效,除非我在字符串的开头查找子字符串.例如,这将输出未找到":

This works in almost all cases, except when I look for the substring at the beginning of the string. For example, this will output "not found" :

$mystring = "/abc/def/hij";
$find = "/abc";

echo(strpos($mystring, $find) . "<br>");
if (strpos($mystring, $find) != false) {
    echo("found");
} else {
    echo("not found");
}

那我该怎么做呢?我只想知道子字符串是否存在于字符串中,如果子字符串是开头或整个字符串,它应该给我"true" ...

So how can I make that work? I just want to know if a substring exists in a string, and it should give me "true" if the substring is the beginning or the entire string...

推荐答案

使用!==运算符进行测试.这将比较类型和值,而不只是值:

Test using the !== operator. This will compare types and values, as opposed to just values:

$mystring = "/abc/def/hij";
$find = "/abc";

echo(strpos($mystring, $find) . "<br>");
if (strpos($mystring, $find) !== false) {
    echo("found");
} else {
    echo("not found");
}

这篇关于关于PHP中的strpos的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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