Strpos 总是给出 true
Strpos always gives true
我有两种类型的 links,它们是从数据库中获取的字符串:
http://www.website.com/anything-else.html
www.website.com/anything-else.html
我需要所有 links 都显示为 http:// 无论如何,所以我使用这个简单的代码来确定 link 是否包含 http,如果没有则添加它:
if (strpos($links, 'http') !== true) {
$linkai = 'http://'.$links;
}
问题是,它正在向任何 link 添加 http://,无论它是否有。
我试过 ==false 等。什么都不管用。有什么想法吗?
试试这个
if (strpos($links, 'http') === false) {
$linkai = 'http://'.$links;
}
在 strpos 文档中说 return 值始终不是布尔值。
“警告
此函数可能 return 布尔值 FALSE,但也可能 return 计算结果为 FALSE 的非布尔值。请阅读有关布尔值的部分以获取更多信息。使用 === 运算符测试此函数的 return 值。"
$arrParsedUrl = parse_url($links);
if (!empty($arrParsedUrl['scheme']))
{
// Contains http:// schema
if ($arrParsedUrl['scheme'] === "http")
{
}
// Contains https:// schema
else if ($arrParsedUrl['scheme'] === "https")
{
}
}
// Don't contains http:// or https://
else
{
$links = 'http://'.$links;
}
echo $links;
我有两种类型的 links,它们是从数据库中获取的字符串:
http://www.website.com/anything-else.html
www.website.com/anything-else.html
我需要所有 links 都显示为 http:// 无论如何,所以我使用这个简单的代码来确定 link 是否包含 http,如果没有则添加它:
if (strpos($links, 'http') !== true) {
$linkai = 'http://'.$links;
}
问题是,它正在向任何 link 添加 http://,无论它是否有。 我试过 ==false 等。什么都不管用。有什么想法吗?
试试这个
if (strpos($links, 'http') === false) {
$linkai = 'http://'.$links;
}
在 strpos 文档中说 return 值始终不是布尔值。
“警告 此函数可能 return 布尔值 FALSE,但也可能 return 计算结果为 FALSE 的非布尔值。请阅读有关布尔值的部分以获取更多信息。使用 === 运算符测试此函数的 return 值。"
$arrParsedUrl = parse_url($links);
if (!empty($arrParsedUrl['scheme']))
{
// Contains http:// schema
if ($arrParsedUrl['scheme'] === "http")
{
}
// Contains https:// schema
else if ($arrParsedUrl['scheme'] === "https")
{
}
}
// Don't contains http:// or https://
else
{
$links = 'http://'.$links;
}
echo $links;