是否有允许我检查URL是否包含特定路径的if语句?
在我的特殊情况下(使用wordpress),我试图检查网址是否包含// https://www.website.com.au/store/brand/,所以在这条路径之后可能会有一些东西.
谢谢你的帮助!
发布于 2018-10-13 10:38:52
我建议使用WordPress函数,如is_singular、is_tax等。
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';
}发布于 2022-04-26 21:21:33
如果您不想创建一个函数,下面是我使用的更简单的选项。
if( strpos( $_SERVER["REQUEST_URI"], "/store/" ) !== false ){ /* found */ }
发布于 2018-10-13 10:31:38
使用strpos()函数。它基本上可以找到一个子字符串在给定字符串中第一个出现的位置。如果找不到子字符串,则返回false。
// 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
}https://stackoverflow.com/questions/52791916
复制相似问题