我有一个标题,基本上是这样说的:
"论坛
一个支持的好地方。"
我需要它显示在我网站的某些页面上,与论坛相关的页面。
然而,在其他页面上,我可能想要一个标题,如:
"捐赠
帮助我们保持在线。"
网站论坛部分的地址将与这些类似。
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
使用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);
}
这样的函数可能会有所帮助。如果您不知道如何从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;
}
}
为您提供的另一个(更优雅的)服务器端解决方案。。。如果你的URL总是在p
参数中显示"路径",你可以利用PHP的explode()
和in_array()
函数来让你的代码更容易处理。以这个URL为例-
http://localhost/index.php?p=/plugin/page/donate
如果我们在$_GET['p']
变量上执行一个explode()
函数,我们将得到这样的数组-
Array(
'plugin',
'page',
'donate'
)
现在您可以执行一个in_array()
函数来查看您要查找的字符串是否存在于这个数组中-
if (in_array('form',explode($_GET['p']){
// we are in the forum!
}
参考资料-
in_array()
explode()
如果您想在服务器端执行此操作,您可以始终使用PHP的strpos()
函数。它将返回一个字符串在另一个字符串中的位置。因此,您所要做的就是检查$_SERVER['query_string']
变量并执行strpos()
搜索-
if (strpos($_SERVER['QUERY_STRING'],'forum')) >= 0){
// forum appears in the query string!
}
strpos()
函数返回要搜索的字符串的索引,因此请记住0
是一个有效的索引。当strpos()
未找到匹配项时。它将返回CCD_ 16。
在这里,我要做的是检查其中一个$_SERVER
变量,它们包含关于服务器的各种信息以及它的当前参数。其中之一是查询字符串,即URL中?
之后的所有文本。一旦我有了这个值,strpos()
函数就会搜索这个值。