繁体   English   中英

根据当前使用的file.php不同的标题

[英]Different titles depending what file.php is currently in use

我需要为正在执行的脚本动态创建标题,标题应取决于当前使用的文件。

我的结构脚本是:

@require_once"bd.php";
@require_once"Funciones/functions.php";
include head.php;
include body.php;
include footer.php;

我的标题功能代码从head.php调用

这是我的功能,但不起作用总是返回空白结果:s

function get_title(){
    $indexurl = "index.php";
    $threadurl = "post.php";
    $searchurl = "search.php";
    $registerurl = "register.php";

    $query = $_SERVER['PHP_SELF'];
    $path = pathinfo( $query );
    $url = $path['basename']; //This returns the php file that is being used

    if(strpos($url,$indexurl)) {
      $title = "My home title";
    }
    if(strpos($url,$threadurl)) {
      $title = "My post title";
    }
    if(strpos($url,$searchurl)) {
      $title = "My search title";
    }
    if(strpos($url,$registerurl)) {
      $title = "My register page title";
    }

return $title;
}

我调用函数:

<title><? echo get_title(); ?></title>

正如我在最初的评论中所说的那样,可以在这里找到更好的方法: https : //stackoverflow.com/a/4858950/1744357

您应该将身份属性与strpos一起使用,并针对FALSE进行测试。

if (strpos($link, $searchterm) !== false) {
  //do stuff here
}

我发现了问题:

$string = "This is a strpos() test";

if(strpos($string, "This)) {
   echo = "found!";
}else{
   echo = "not found";
}

如果您尝试执行该命令,尽管$ string中的“ This”很明显,它仍将输出“ Not found”。 这是另一个区分大小写的问题吗? 不完全的。 这次的问题在于,“ this”是$ string中的第一件事,这意味着strpos()将返回0。但是,PHP将0视为与false相同的值,这意味着我们的if语句不能告诉“未找到子字符串”和“在索引0找到子字符串”之间的区别-相当麻烦!

因此,在我的情况下,使用strpos的正确方法是从$ indexurl,$ threadurl,$ searchurl和$ registerurl中删除第一个字符

function get_title(){
    $indexurl = "ndex.php";
    $threadurl = "ost.php";
    $searchurl = "earch.php";
    $registerurl = "egister.php";

    $query = $_SERVER['PHP_SELF'];
    $path = pathinfo( $query );
    $url = $path['basename']; //This returns the php file that is being used

    if(strpos($url,$indexurl)) {
      $title = "My home title";
    }
    if(strpos($url,$threadurl)) {
      $title = "My post title";
    }
    if(strpos($url,$searchurl)) {
      $title = "My search title";
    }
    if(strpos($url,$registerurl)) {
      $title = "My register page title";
    }

return $title;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM