0

I need to create titles dynamically for a script that I'm doing, the title should be depending on the file that is in use currently.

My structure script is:

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

My title function code is called from head.php

this is my function, but not works always returns blank result :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;
}

Im call the function:

<title><? echo get_title(); ?></title>
jose sanchez
  • 339
  • 4
  • 12
  • What is $url actually returning? Have you tested that it does return the php file name? – francisco.preller Oct 14 '13 at 01:25
  • Yes the $url returns correctly: index.php, post.php, search.php and register.php depending what file is in use. – jose sanchez Oct 14 '13 at 01:27
  • You should probably do `strpos($url, $indexurl) !== FALSE` as opposed to assuming it returns an integer. Since if its the first occurrence strpos will return 0 which your if statement translates to false. – rocket_boomerang_19 Oct 14 '13 at 01:40
  • and also setup your if statements such that: if...else if...else if...elseif...else. So it stops processing after the first occurrence and has a default in the else statement if a value is not found – rocket_boomerang_19 Oct 14 '13 at 01:42

2 Answers2

2

A better approach can be found here as a I said in my initial comment: https://stackoverflow.com/a/4858950/1744357

You should use the identity property with strpos and test against FALSE.

if (strpos($link, $searchterm) !== false) {
  //do stuff here
}
Community
  • 1
  • 1
rocket_boomerang_19
  • 519
  • 1
  • 3
  • 19
-1

I found the problem:

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

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

If you try executing that, you will find that it outputs "Not found", despite "This" quite clearly being in $string. Is it another case sensitivity problem? Not quite. This time the problem lies in the fact that "This" is the first thing in $string, which means that strpos() will return 0. However, PHP considers 0 to be the same value as false, which means that our if statement cannot tell the difference between "Substring not found" and "Substring found at index 0" - quite a problem!.

So, The correct way to use strpos in my case is removing the first character from $indexurl,$threadurl,$searchurl and $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;
}
jose sanchez
  • 339
  • 4
  • 12
  • You're doing it wrong. See other answer above. http://stackoverflow.com/a/19353179/472768 – FeifanZ Oct 14 '13 at 17:17
  • can you test the code inside that link?, not works, the first character don't takes into consideration. – jose sanchez Oct 14 '13 at 17:30
  • I'm using that exact code in the same situation on a production site. Not sure what "the first character don't takes into consideration" means – FeifanZ Oct 14 '13 at 17:49