Echo filename in <title>

Echo filename in <title>

am 24.09.2007 02:17:15 von geetarista

I know this is probably very simple, but for some reason I can't get
it to work. Basically, I want to use the filename of a web page
within the tags in the page <head>. So, if the filename is<br /> pictures.php, I want the title to be "Root title - Pictures", where<br /> the word "Pictures" is generated by PHP. So I just need PHP to look<br /> up the filename without the extension, capitalize the first letter,<br /> and then have the ability to call it within <title>.</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 03:02:17 von Steve</span> <p>"geetarista" <geetarista@gmail.com> wrote in message <br /> news:1190593035.326271.211030@22g2000hsm.googlegroups.com...<br /> >I know this is probably very simple, but for some reason I can't get<br /> > it to work. Basically, I want to use the filename of a web page<br /> > within the <title> tags in the page <head>. So, if the filename is<br /> > pictures.php, I want the title to be "Root title - Pictures", where<br /> > the word "Pictures" is generated by PHP. So I just need PHP to look<br /> > up the filename without the extension, capitalize the first letter,<br /> > and then have the ability to call it within <title>.<br /> <br /> <?<br /> $pageTitle = 'Root Title - Pictures';<br /> ?><br /> <title><?= $pageTitle ?>

using the file name to derive text (other than literal 'pictures.php') is
voodoo magic at its finest...don't do it. the above is the simplest way to
implement what you want. you could also use a db to store the name and look
it up (use the script name, like $_SERVER['PHP_SELF'] as the key to the db
lookup).

if you do this the way you suggest, you've required that anyone who
developes pages on your site to somehow know they have to do something
special when naming scripts. file system, db, html, php are all different
layers of an application and each should be as self-contained, independent,
and isolated as possible. a good read for you would be 'ISO 7 layers' and
'code complete'.

if you still insist...

$scriptName = !is_link(__FILE__) ? __FILE__ : readlink(__FILE__);
$pathInfo = pathinfo($scriptName);
$scriptName = ucwords(strtolower($pathInfo['filename']));
?>
<?= $scriptName ?>

as you can see, the first example is much more straight-forward.

Re: Echo filename in <title>

am 24.09.2007 03:04:30 von Michael Fesser

..oO(geetarista)

>I know this is probably very simple, but for some reason I can't get
>it to work. Basically, I want to use the filename of a web page
>within the tags in the page <head>. So, if the filename is<br /> >pictures.php, I want the title to be "Root title - Pictures", where<br /> >the word "Pictures" is generated by PHP. So I just need PHP to look<br /> >up the filename without the extension, capitalize the first letter,<br /> >and then have the ability to call it within <title>.<br /> <br /> $_SERVER['PHP_SELF'], pathinfo() and ucfirst() should give you all you<br /> need. See the manual for details.<br /> <br /> Micha</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 03:58:32 von Macca</span> <p>function filename_asuc(){<br /> <br /> $last_dot = strlen(strchr($_SERVER['PHP_SELF'], '.'));<br /> <br /> $dir = explode('/', substr($_SERVER['PHP_SELF'], 0, - $last_dot));<br /> <br /> $sep = count($dir) - 1;<br /> <br /> return ucfirst($dir[$sep]);<br /> <br /> }<br /> <br /> <br /> // call using this<br /> echo filename_asuc();</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 04:13:27 von geetarista</span> <p>On Sep 23, 6:58 pm, macca <ptmcna...@googlemail.com> wrote:<br /> > function filename_asuc(){<br /> ><br /> > $last_dot = strlen(strchr($_SERVER['PHP_SELF'], '.'));<br /> ><br /> > $dir = explode('/', substr($_SERVER['PHP_SELF'], 0, - $last_dot));<br /> ><br /> > $sep = count($dir) - 1;<br /> ><br /> > return ucfirst($dir[$sep]);<br /> ><br /> > }<br /> ><br /> > // call using this<br /> > echo filename_asuc();<br /> <br /> Thanks everyone! I didn't see the last two posts while I was working<br /> on this, but this is what I came up with:<br /> <br /> <?php<br /> $path = $_SERVER['SCRIPT_FILENAME'];<br /> $file = basename($path, '.php');<br /> $fileName = ucfirst($file);<br /> ?><br /> <title>Company Name - <?php echo $fileName ?>

The purpose of this is so that I can include the tags in a<br /> file called head.php for every page within a web site. This way, I<br /> don't have to worry about changing or adding the title every time. I<br /> just include it as part of head.php and will even work if I change the<br /> name of a file. So when you go to pictures.php, equipment.php,<br /> jobs.php, or whatever, it will show the correct <title> automatically.<br /> <br /> Thanks again for your help and pointing me in the right direction.<br /> Let me know if this is OK to do and if I should even do it this way.<br /> I just thought it might make it a little easier for me and others that<br /> might work on the project. All of the meta tags, scripts, and style<br /> sheets are the same, so I want to just include DOCTYPE and <head> in<br /> my head.php.</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 04:23:50 von Steve</span> <p>"macca" <ptmcnally@googlemail.com> wrote in message <br /> news:1190599112.853214.48850@r29g2000hsg.googlegroups.com...<br /> > function filename_asuc(){<br /> ><br /> > $last_dot = strlen(strchr($_SERVER['PHP_SELF'], '.'));<br /> ><br /> > $dir = explode('/', substr($_SERVER['PHP_SELF'], 0, - $last_dot));<br /> ><br /> > $sep = count($dir) - 1;<br /> ><br /> > return ucfirst($dir[$sep]);<br /> ><br /> > }<br /> ><br /> ><br /> > // call using this<br /> > echo filename_asuc();<br /> <br /> that's all great...until you have a file name without an extension.</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 04:30:45 von Steve</span> <p>"geetarista" <geetarista@gmail.com> wrote in message <br /> news:1190600007.908809.136130@19g2000hsx.googlegroups.com...<br /> > On Sep 23, 6:58 pm, macca <ptmcna...@googlemail.com> wrote:<br /> >> function filename_asuc(){<br /> >><br /> >> $last_dot = strlen(strchr($_SERVER['PHP_SELF'], '.'));<br /> >><br /> >> $dir = explode('/', substr($_SERVER['PHP_SELF'], 0, - $last_dot));<br /> >><br /> >> $sep = count($dir) - 1;<br /> >><br /> >> return ucfirst($dir[$sep]);<br /> >><br /> >> }<br /> >><br /> >> // call using this<br /> >> echo filename_asuc();<br /> ><br /> > Thanks everyone! I didn't see the last two posts while I was working<br /> > on this, but this is what I came up with:<br /> ><br /> > <?php<br /> > $path = $_SERVER['SCRIPT_FILENAME'];<br /> > $file = basename($path, '.php');<br /> > $fileName = ucfirst($file);<br /> > ?><br /> > <title>Company Name - <?php echo $fileName ?>
>
> The purpose of this is so that I can include the tags in a<br /> > file called head.php for every page within a web site. This way, I<br /> > don't have to worry about changing or adding the title every time. I<br /> > just include it as part of head.php and will even work if I change the<br /> > name of a file. So when you go to pictures.php, equipment.php,<br /> > jobs.php, or whatever, it will show the correct <title> automatically.<br /> <br /> voodoo it is.<br /> <br /> > Thanks again for your help and pointing me in the right direction.<br /> > Let me know if this is OK to do and if I should even do it this way.<br /> <br /> i did...seems you didn't read my post. the answer is HELL NO!<br /> <br /> > I just thought it might make it a little easier for me and others that<br /> > might work on the project.<br /> <br /> quite the opposite effect.<br /> <br /> > All of the meta tags, scripts, and style<br /> > sheets are the same, so I want to just include DOCTYPE and <head> in<br /> > my head.php.<br /> <br /> well, that kind of negates the benefit of having a head.php...don't it. you <br /> should typically include everything up to, and including, <body> ... the <br /> rest is your page specific output.<br /> <br /> define whatever you need at the top of the script (variables that are used <br /> in head.php), then do the require/include. your argument about the page name <br /> and file name is mute. even if you change the file name yet have a variable <br /> like $pageTitle that you work from, i bet your script won't morph into <br /> something different than its original purpose...i.e. Pictures. but again, <br /> see my arguments in the previous post. this is bad news.</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 04:37:38 von geetarista</span> <p>On Sep 23, 7:30 pm, "Steve" <no....@example.com> wrote:<br /> > "geetarista" <geetari...@gmail.com> wrote in message<br /> ><br /> > news:1190600007.908809.136130@19g2000hsx.googlegroups.com...<br /> ><br /> ><br /> ><br /> > > On Sep 23, 6:58 pm, macca <ptmcna...@googlemail.com> wrote:<br /> > >> function filename_asuc(){<br /> ><br /> > >> $last_dot = strlen(strchr($_SERVER['PHP_SELF'], '.'));<br /> ><br /> > >> $dir = explode('/', substr($_SERVER['PHP_SELF'], 0, - $last_dot));<br /> ><br /> > >> $sep = count($dir) - 1;<br /> ><br /> > >> return ucfirst($dir[$sep]);<br /> ><br /> > >> }<br /> ><br /> > >> // call using this<br /> > >> echo filename_asuc();<br /> ><br /> > > Thanks everyone! I didn't see the last two posts while I was working<br /> > > on this, but this is what I came up with:<br /> ><br /> > > <?php<br /> > > $path = $_SERVER['SCRIPT_FILENAME'];<br /> > > $file = basename($path, '.php');<br /> > > $fileName = ucfirst($file);<br /> > > ?><br /> > > <title>Company Name - <?php echo $fileName ?>
>
> > The purpose of this is so that I can include the tags in a<br /> > > file called head.php for every page within a web site. This way, I<br /> > > don't have to worry about changing or adding the title every time. I<br /> > > just include it as part of head.php and will even work if I change the<br /> > > name of a file. So when you go to pictures.php, equipment.php,<br /> > > jobs.php, or whatever, it will show the correct <title> automatically.<br /> ><br /> > voodoo it is.<br /> ><br /> > > Thanks again for your help and pointing me in the right direction.<br /> > > Let me know if this is OK to do and if I should even do it this way.<br /> ><br /> > i did...seems you didn't read my post. the answer is HELL NO!<br /> ><br /> > > I just thought it might make it a little easier for me and others that<br /> > > might work on the project.<br /> ><br /> > quite the opposite effect.<br /> ><br /> > > All of the meta tags, scripts, and style<br /> > > sheets are the same, so I want to just include DOCTYPE and <head> in<br /> > > my head.php.<br /> ><br /> > well, that kind of negates the benefit of having a head.php...don't it. you<br /> > should typically include everything up to, and including, <body> ... the<br /> > rest is your page specific output.<br /> ><br /> > define whatever you need at the top of the script (variables that are used<br /> > in head.php), then do the require/include. your argument about the page name<br /> > and file name is mute. even if you change the file name yet have a variable<br /> > like $pageTitle that you work from, i bet your script won't morph into<br /> > something different than its original purpose...i.e. Pictures. but again,<br /> > see my arguments in the previous post. this is bad news.<br /> <br /> I see where you're coming from, Steve. I did read your first post,<br /> but I thought that what I was doing was a little different. However,<br /> I realize now a little better what you were trying to say and I'm<br /> going to do it your way. It will probably be a little easier that way<br /> and I'll have a little more freedom. I'll just define the name of the<br /> title before I include head.php--makes perfect sense! Sometimes I<br /> guess I make it a little harder than it needs to be... ;)<br /> <br /> Thanks again!<br /> <br /> p.s. - It's moot--not mute.</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 04:57:22 von Steve</span> <p>"geetarista" <geetarista@gmail.com> wrote in message <br /> news:1190601458.484015.218990@19g2000hsx.googlegroups.com...<br /> > On Sep 23, 7:30 pm, "Steve" <no....@example.com> wrote:<br /> >> "geetarista" <geetari...@gmail.com> wrote in message<br /> >><br /> >> news:1190600007.908809.136130@19g2000hsx.googlegroups.com...<br /> >><br /> >><br /> >><br /> >> > On Sep 23, 6:58 pm, macca <ptmcna...@googlemail.com> wrote:<br /> >> >> function filename_asuc(){<br /> >><br /> >> >> $last_dot = strlen(strchr($_SERVER['PHP_SELF'], '.'));<br /> >><br /> >> >> $dir = explode('/', substr($_SERVER['PHP_SELF'], 0, - $last_dot));<br /> >><br /> >> >> $sep = count($dir) - 1;<br /> >><br /> >> >> return ucfirst($dir[$sep]);<br /> >><br /> >> >> }<br /> >><br /> >> >> // call using this<br /> >> >> echo filename_asuc();<br /> >><br /> >> > Thanks everyone! I didn't see the last two posts while I was working<br /> >> > on this, but this is what I came up with:<br /> >><br /> >> > <?php<br /> >> > $path = $_SERVER['SCRIPT_FILENAME'];<br /> >> > $file = basename($path, '.php');<br /> >> > $fileName = ucfirst($file);<br /> >> > ?><br /> >> > <title>Company Name - <?php echo $fileName ?>
>>
>> > The purpose of this is so that I can include the tags in a<br /> >> > file called head.php for every page within a web site. This way, I<br /> >> > don't have to worry about changing or adding the title every time. I<br /> >> > just include it as part of head.php and will even work if I change the<br /> >> > name of a file. So when you go to pictures.php, equipment.php,<br /> >> > jobs.php, or whatever, it will show the correct <title> automatically.<br /> >><br /> >> voodoo it is.<br /> >><br /> >> > Thanks again for your help and pointing me in the right direction.<br /> >> > Let me know if this is OK to do and if I should even do it this way.<br /> >><br /> >> i did...seems you didn't read my post. the answer is HELL NO!<br /> >><br /> >> > I just thought it might make it a little easier for me and others that<br /> >> > might work on the project.<br /> >><br /> >> quite the opposite effect.<br /> >><br /> >> > All of the meta tags, scripts, and style<br /> >> > sheets are the same, so I want to just include DOCTYPE and <head> in<br /> >> > my head.php.<br /> >><br /> >> well, that kind of negates the benefit of having a head.php...don't it. <br /> >> you<br /> >> should typically include everything up to, and including, <body> ... the<br /> >> rest is your page specific output.<br /> >><br /> >> define whatever you need at the top of the script (variables that are <br /> >> used<br /> >> in head.php), then do the require/include. your argument about the page <br /> >> name<br /> >> and file name is mute. even if you change the file name yet have a <br /> >> variable<br /> >> like $pageTitle that you work from, i bet your script won't morph into<br /> >> something different than its original purpose...i.e. Pictures. but again,<br /> >> see my arguments in the previous post. this is bad news.<br /> ><br /> > I see where you're coming from, Steve. I did read your first post,<br /> > but I thought that what I was doing was a little different. However,<br /> > I realize now a little better what you were trying to say and I'm<br /> > going to do it your way. It will probably be a little easier that way<br /> > and I'll have a little more freedom. I'll just define the name of the<br /> > title before I include head.php--makes perfect sense! Sometimes I<br /> > guess I make it a little harder than it needs to be... ;)<br /> <br /> not 'my way' actually. that's the most typical implementation i've come <br /> across, so i assumed you were doing it in a similar fashion.<br /> <br /> > Thanks again!<br /> <br /> np.<br /> <br /> > p.s. - It's moot--not mute.<br /> <br /> i know...i think much faster than my fingers can type. plus, i usually don't <br /> spell check in this forum either. hey, at least i didn't leave out entire <br /> words in this one. ;^)<br /> <br /> cheers.</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 05:41:10 von Macca</span> <p>>that's all great...until you have a file name without an extension.<br /> <br /> <br /> Bah! Not likely to have a web page (especially one with php in it)<br /> without an extension though is he?<br /> <br /> <br /> Sigh. Some people just want to argue for the sake of it.</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 24.09.2007 06:40:13 von Steve</span> <p>"macca" <ptmcnally@googlemail.com> wrote in message <br /> news:1190605270.881192.115760@19g2000hsx.googlegroups.com...<br /> > >that's all great...until you have a file name without an extension.<br /> ><br /> ><br /> > Bah! Not likely to have a web page (especially one with php in it)<br /> > without an extension though is he?<br /> ><br /> ><br /> > Sigh. Some people just want to argue for the sake of it.<br /> <br /> no, some people care not to make such dumbass assumptions. especially when <br /> the solutions posted didn't even REQUIRE coming close to shooting their own <br /> foot.<br /> <br /> sigh. some people can't see simplicity through their own ego. bruise easily?</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 25.09.2007 02:16:25 von Macca</span> <p>>From the original post:<br /> <br /> Basically, I want to use the filename of a web page<br /> within the <title> tags in the page <head>.<br /> <snip><br /> So I just need PHP to look<br /> up the filename without the extension, capitalize the first letter,<br /> and then have the ability to call it within <title>.<br /> <br /> <br /> My function soes exactly what he asked for.</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 25.09.2007 04:49:32 von cresh</span> <p>On Sep 24, 7:16 pm, macca <ptmcna...@googlemail.com> wrote:<br /> > >From the original post:<br /> ><br /> > Basically, I want to use the filename of a web page<br /> > within the <title> tags in the page <head>.<br /> > <snip><br /> > So I just need PHP to look<br /> > up the filename without the extension, capitalize the first letter,<br /> > and then have the ability to call it within <title>.<br /> ><br /> > My function soes exactly what he asked for.<br /> <br /> Seems that you've gotten this figured out. I'd only add to put all<br /> your <head> variables in one file and call it with INCLUDE or REQUIRE<br /> as someone above me already stated. There's a lot of reasons for this,<br /> the best being less room for error. Create one file, run many, many<br /> pages with it.<br /> <br /> Great stuff.<br /> <br /> Cresh</p> </article> <article> <h2>Re: Echo filename in <title></h2><span>am 25.09.2007 05:33:07 von Steve</span> <p>"macca" <ptmcnally@googlemail.com> wrote in message <br /> news:1190679385.207430.268930@r29g2000hsg.googlegroups.com.. .<br /> > >From the original post:<br /> ><br /> > Basically, I want to use the filename of a web page<br /> > within the <title> tags in the page <head>.<br /> > <snip><br /> > So I just need PHP to look<br /> > up the filename without the extension, capitalize the first letter,<br /> > and then have the ability to call it within <title>.<br /> ><br /> ><br /> > My function soes exactly what he asked for.<br /> <br /> only if that file name has an extension. follow the logic...just because he <br /> says 'without the extension', it certainly does not mean that all of his <br /> files *have* extensions. you do not have enough information to assume <br /> anything else correctly. also, your function doesn't account for sym-links - <br /> a lot of which DON'T have extensions...by choice.<br /> <br /> anyway, say what you want. i just know that if you try and throw a wrench <br /> (an exception to the rule) at what i wrote, my example will still work. <br /> notice that i also ucwords()...since i think about the result he wants <br /> rather than just the details he provides. i'd hate to have to go in and <br /> modify this thing the first time someone named a file 'free pictures.php'. <br /> good thing i wouldn't have to.<br /> <br /> ;^)</p> </article> <footer> <a href="/">Index</a> | <a href="/impressum.php">Impressum</a> | <a href="/datenschutz.php">Datenschutz</a> | <a href="https://www.xodox.de/">XODOX</a> </footer> </main> </body> </html>