首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >语句,检查URL是否包含特定路径?

语句,检查URL是否包含特定路径?
EN

Stack Overflow用户
提问于 2018-10-13 10:23:04
回答 3查看 4.1K关注 0票数 4

是否有允许我检查URL是否包含特定路径的if语句?

在我的特殊情况下(使用wordpress),我试图检查网址是否包含// https://www.website.com.au/store/brand/,所以在这条路径之后可能会有一些东西.

谢谢你的帮助!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-10-13 10:38:52

我建议使用WordPress函数,如is_singular、is_tax等。

代码语言:javascript
运行
复制
function get_current_url()
{
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "۸۰") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

$url = get_current_url();

if (strpos($url, '/store/') !== false) {
    echo 'found';
}else{
    echo 'not found';
}
票数 5
EN

Stack Overflow用户

发布于 2022-04-26 21:21:33

如果您不想创建一个函数,下面是我使用的更简单的选项。

if( strpos( $_SERVER["REQUEST_URI"], "/store/" ) !== false ){ /* found */ }

票数 3
EN

Stack Overflow用户

发布于 2018-10-13 10:31:38

使用strpos()函数。它基本上可以找到一个子字符串在给定字符串中第一个出现的位置。如果找不到子字符串,则返回false

代码语言:javascript
运行
复制
// input url string
$url = 'https://www.website.com.au/store/brand/';

$path_to_check_for = '/store/';

// check if /store/ is the url string
if ( strpos($url, $path_to_check_for)  !== false ) {

    // Url contains the desired path string
    // your remaining code to do something in case path string is there

} else {

    // Url does not contain the desired path string
    // your remaining code to do something in case path string is not there
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52791916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档