0

我有一个标题,基本上是这样说的:

" 论坛

支持的好地方。"

我需要它显示在我网站的某些页面上,这些页面与论坛相关。

但是在其他页面上,我可能想要一个标题,例如:

" 捐赠

帮助我们保持在线。"

网站论坛部分的地址与这些类似。

http://localhost/index.php?p=/discussions
http://localhost/index.php?p=/activity
http://localhost/index.php?p=/discussion/6/oh-dear#Item_1

捐赠的可能是这样的:

http://localhost/index.php?p=/plugin/page/donate

所以我需要一些方法来编写一个脚本

if url has (discussions, activity, discussion)
then use this header
"<b>Forum<b> <br> a great place for support

if else url has (donate)
then use this header
"<b>Donate<b> <br> help keep us online

else
use this header
"<b>Website<b> <br> this is our website
4

4 回答 4

0

另一个(更优雅的)服务器端解决方案适合您...如果您的 URL 总是出现在p参数中的“路径”,您可以利用 PHPexplode()in_array()函数来使您的代码更易于处理。以这个网址为例 -

http://localhost/index.php?p=/plugin/page/donate

如果我们explode()对变量执行一个函数$_GET['p'],我们会得到一个像这样的数组 -

Array(
  'plugin',
  'page',
  'donate'
)

现在您可以执行一个in_array()函数来查看您要查找的字符串是否存在于该数组中 -

if (in_array('form',explode($_GET['p']){
  // we are in the forum!
}

参考 -

于 2012-09-13T19:49:16.507 回答
0

像这样的功能可能会有所帮助。如果您不知道如何从 url 获取变量,请使用 $_GET['p']

function contains($substring, $string) {
    $pos = strpos($string, $substring);

    if($pos === false) {
            // string needle NOT found in haystack
            return false;
    }
    else {
            // string needle found in haystack
            return true;
    }

}
于 2012-09-13T19:33:22.050 回答
0

如果你想在服务器端做这个,你总是可以使用 PHP 的strpos()函数。它将返回一个字符串在另一个字符串中的位置。因此,您所要做的就是检查$_SERVER['query_string']变量并执行strpos()搜索 -

if (strpos($_SERVER['QUERY_STRING'],'forum')) >= 0){
  // forum appears in the query string!
}

strpos()函数返回您正在搜索的字符串的索引,因此请记住这0是一个有效的索引。什么strpos()时候找不到匹配项。它会回来false的。

在这里,我正在检查$_SERVER变量之一,它们包含有关服务器的各种信息以及它的当前参数是什么。其中之一是查询字符串 - 这是?URL 中的所有文本。一旦我有了它,该strpos()函数将搜索该值中的某些内容。

于 2012-09-13T19:34:17.617 回答
0

使用 Javascriptlocation对象:

url = location.href;
if (url.indexOf('discussions') && url.indexOf('activity') && url.indexOf('discussion')) {
  document.getElementById('parent').appendChild(child-element);
else if (url.indexOf('donate')) {
  document.getElementById('parent').appendChild(other-child-element);
}
else {
 document.getElementById('parent').appendChild(another-child-element);
}
于 2012-09-13T19:31:28.767 回答