簡體   English   中英

如果當前頁面網址等於X或X?

[英]If current page URL equals X OR X?

我在internetz上找到了這個代碼,它檢查當前頁面的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;
}

所以現在我可以做這樣的事情;

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

大。 但我也想將它用於分頁頁面。 這些網址如下所示:

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

我可以為每個鏈接使用if語句..但我更願意使用一個。 是否可以添加通配符或其他內容? 像這樣我猜(注意*

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

編輯 :我想為所有這些URL執行此操作,因為我想給它們相同的meta description<title><link rel="canonical" 我可以通過為每個頁面(10 + atm)執行if語句來手動執行此操作,但我認為有更好的方法。

為什么不直接使用parse_url()函數? 從手冊頁:

<?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
)

對於您的特定情況,您可以檢查hostpath變量。

聽起來很像正則表達式問題:

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

表達式[^&]*就像你的* ; to match non-empty items, use ; to match non-empty items, use [^&] +`。 它匹配這些:

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

更新

除非您有多個域指向相同的代碼庫,否則您需要與完整的規范URL進行比較並不完全清楚。

您應該使用字符串比較功能

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

要么

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

有很多方法可以做到這一點

你可以包裝它

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

在while循環中,為每個迭代中都有通配符的變量添加30。

你試過正則表達式嗎?

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.";
}

如果您不使用$ _SERVER數組中的query_string元素,則所有分頁的URL都將返回相同的URL: http//www.example.com/pageexample ,您可以使用

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

VS

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

你會看到在第一種情況下你沒有收到GET參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM