If current page URL equals X OR X?

柔情痞子 提交于 2019-12-12 13:07:10

问题


I found this code on the internetz, it checks the current page url;

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

So now I can do something like this;

elseif (curPageURL() == "http://www.example.com/pageexample") {
<meta tags here>
}

Great. But I would also like to use this for pagination pages. Those URLs look like this:

http://www.example.com/pageexample?start=30&groep=0
http://www.example.com/pageexample?start=60&groep=0
http://www.example.com/pageexample?start=90&groep=0
[....]
http://www.example.com/pageexample?start=270&groep=0

I could use a if statement for every of those links.. but I would much rather like to use one. Is it possible to add a wildcard or something? Like this I guess (notice the *)

elseif (curPageURL() == "http://www.example.com/pageexample" OR curPageURL() == "http://www.example.com/pageexample?start=*&groep=0") {

edit: I would like to do this for all those URLs because I want to give them the same meta description, <title> and <link rel="canonical". I could do this manually by doing an if-statement for every page (10+ atm) but I figured there was a better way.


回答1:


Sounds a lot like regex problem:

if (preg_match("#^http://www.example.com/pageexample(\?start=[^&]*&groep=0)?#", curPageURL())) {
    // it matches
}

The expression [^&]* acts like your *.; to match non-empty items, use[^&]+`. It matches these:

http://www.example.com/pageexample
http://www.example.com/pageexample?start=30&groep=0

Update

It's not entirely clear why you need to compare against the full canonical URL, unless you have multiple domains point to the same code base.




回答2:


Why not just use the parse_url() function? From the manual page:

<?php

$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));

?>

// The above would print
Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

For your particular case, you could then just check against the host and path variables.




回答3:


You should use a string comparison function

if (strstr(curPageURL(), 'http://www.example.com/')) !== FALSE) {
  // curPageURL() contains http://www.example.com/
}

or

if (preg_match('/^http\:\/\/www\.example\.com\//', curPageURL()) { 
  // curPageURL() starts with http://www.example.com/
}

There's lots of ways to do it




回答4:


You could wrap this

elseif (curPageURL() == "http://www.example.com/pageexample" OR curPageURL() == "http://www.example.com/pageexample?start=*&groep=0") {

in a while loop that adds 30 to a variable where you have your wild card on each iteration.




回答5:


did you try regex?

if (preg_match('/http:\/\/www\.example\.com\/pageexample\?start=[0-9]+&groep\=0/i', "http://www.example.com/pageexample?start=34&groep=0")) {
   echo "A match was found.";
else {
   echo "A match was not found.";
}



回答6:


If you don't use the query_string element from the $_SERVER array all your paginated URLs will return the same URL: http://www.example.com/pageexample, you can check with

echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] ;

vs

echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'?'.$_SERVER["QUERY_STRING"] ;

You'll see that in the first case you don't receive GET parameters



来源:https://stackoverflow.com/questions/10997897/if-current-page-url-equals-x-or-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!