Page <title> from Page Name

Discussion in 'PHP' started by JosS, Sep 29, 2006.

  1. #1
    Say, I had a page called

    www.mysite.com/download/page_is_good.html


    How would I go about making the <title> of the page like this

    <title>Page Is Good</title> from the URL?

    So it runs through the page link, and then makes the title whatever the page name is minus the _ and .html and capitilises the words?

    I seen it was done before, but can't find the thread I read a while back
     
    JosS, Sep 29, 2006 IP
  2. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi!

    Not sure what you are after. Something like this:
    <title><?php echo ucwords(str_replace('_', ' ', $_SERVER['PHP_SELF'])) ?></title>
    PHP:
    Or different?

    Regards
    - P
     
    penagate, Sep 30, 2006 IP
  3. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This works,

    however it echos /download.php

    instead of say "cool icon" from /cool_icon.html

    I think its because of .htacess.

    Anyway around this?
     
    JosS, Oct 2, 2006 IP
  4. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    basename(ucwords(str_replace('_', ' ', $_SERVER['PHP_SELF'])))
     
    penagate, Oct 2, 2006 IP
    JosS likes this.
  5. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hmm, thought that worked, but nope.

    What that new code did was just get rid of the / before download.php

    So now it echos download.php as the title instead of /download.php

    ---

    This is how the script works.

    I have in the base directory

    download.php

    then a directory called
    download/

    download.php does all the work, however, in download directory is a .htaccess file that make things like this

    download.php?cat=2?id=3 look like /download/cool_graphic_3.html

    I need it to echo,

    "cool graphic 3" in the title if that were the case.

    Cheers penagate for the help, I really appreciate it and gave you some rep! :)
     
    JosS, Oct 2, 2006 IP
  6. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #6
    My mistake.

    Try:
    $f = ucwords(str_replace('_', ' ', basename($_SERVER['PHP_SELF'])));
    $f = substr($f, 0, strrpos($f, '.'));
    
    PHP:
    Regards
    - P
     
    penagate, Oct 2, 2006 IP
  7. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry,

    how would I incorporate that into the title tag?
     
    JosS, Oct 2, 2006 IP
  8. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Put that snippet in a <?php ... ?> block somewhere at the top of your page before the HTML and then use this line:
    <title><?php echo $f ?></title>
    Code (markup):
     
    penagate, Oct 2, 2006 IP
  9. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #9
    Just FYI, you can also use an equivalent of echo for small outputs like this:
    
    <title><?=$f?></title>
    
    PHP:
     
    wmtips, Oct 2, 2006 IP
  10. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Hmm,

    that just makes it say "download" now.

    Sorry, anything else!

    We're making progress though eliminating the ones that don't work! hehe :)

    Thanks again for the help!
     
    JosS, Oct 2, 2006 IP
  11. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You might be able to on your installation, but I strongly advise against it. Short tags are disabled by default in PHP 5, and also by many web hosts, as they conflict with XML processing instructions. Thus, you should never use them in a production situation, as they are not portable code. Also, I believe they will be removed in PHP 6.

    It says "Download" for me with a file "download.php".

    Put echo('Basename: ' . basename($_SERVER['PHP_SELF'])) before the $f lines and tell me what that outputs.
     
    penagate, Oct 2, 2006 IP
  12. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #12
    You are wrong. short_open_tag turned on by default in php 5 and all php > 4.0 (http://www.php.net/ini.core). This flag can be always changed with .htaccess or ini_set if it is disabled by hoster.
     
    wmtips, Oct 3, 2006 IP
  13. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Refer:
    The other two methods are not reliable, and that's from personal experience.
     
    penagate, Oct 3, 2006 IP