根据网址中的字词显示不同的文字

时间:2012-09-13 19:28:41

标签: php html web

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

” 的论坛

支持的好地方。 “

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

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

” 的捐赠

帮助我们保持在线状态。 “

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

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 个答案:

答案 0 :(得分:0)

使用Javascript location对象:

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);
}

答案 1 :(得分: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;
    }

}

答案 2 :(得分:0)

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

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

strpos()函数返回您要搜索的字符串的索引,因此请记住 0是有效索引strpos()找不到匹配项时它将返回false

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

答案 3 :(得分:0)

另一个(更优雅的)服务器端解决方案...如果您的网址始终显示在p参数中的“路径”,则可以使用PHP的explode()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!
}

参考文献 -

相关问题