我有以下三种可能的网址..
www.mydomain.com/445部分是动态生成的,每次都不同,所以我不能完全匹配,我怎样才能检测到以下内容......
我尝试的一切都失败了,无论它总是会检测到登录的部分......
if(strpos($referrer, '?status=empty')) {
echo 'The status is empty';
}
elseif(strpos($referrer, '?status=complete')) {
echo 'The status is complete';
}
elseif(strpos($referrer, '/loggedin/')) {
echo 'The status is loggedin';
}
答案 0 :(得分:1)
将网址分割成细分
$path = explode('/',$referrer);
$path = array_slice($path,1);
然后只需在该数组上使用您的逻辑,您包含的第一个URL就会返回:
Array ( [0] => 445 [1] => loggedin [2] => ?status=empty )
答案 1 :(得分:1)
你可以这样做:
$referrer = 'www.mydomain.com/445/loggedin/?status=empty';
// turn the referrer into an array, delimited by the /
$url = explode('/', $referrer);
// the statuses we check against as an array
$statuses = array('?status=complete', '?status=empty');
// If "loggedin" is found in the url, and count the array_intersect matches, if the matches = 0, none of the statuses you specified where found
if( in_array('loggedin', $url) && count(array_intersect($url, $statuses)) == 0 )
{
echo 'The user is logged in';
}
// if the complete status exists in the url
else if( in_array('?status=complete', $url) )
{
echo 'The status is complete';
}
// if the empty status exists in the url
else if( in_array('?status=empty', $url) )
{
echo 'The status is empty';
}
我建议查看array_intersect
,这非常有用。
希望它有所帮助,不确定这是否是最佳方式,但可能会激发你的想象力。
答案 2 :(得分:0)
Strpos可能不是您想要使用的。你可以用stristr:
来做 if($test_str = stristr($referrer, '/loggedin/'))
{
if(stristr($test_str, '?status=empty'))
{
echo 'empty';
}
elseif (stristr($test_str, '?status=complete'))
{
echo 'complete';
} else {
echo 'logged in';
}
}
但使用正则表达式可能更容易/更好:
if(preg_match('/\/loggedin\/(\?status=(.+))?$/', $referrer, $match))
{
if(count($match)==2) echo "The status is ".$match[2];
else echo "The status is logged in";
}