簡體   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