Hello. I have a web site to fix and clean up, and it's in php includes. Some documents currently have keywords, titles and description defined in each individual page by variables, and these are then called into the header.php file, so: <title><?php echo $title ?></title> Ok, remember how I said some of the pages? Well there are about 2000 perhaps, 3000 pages that DON'T! I'd like to somehow use a find and replace type thing in dreamweaver and where the title of the page is in the header tags, I'd like to surround that with something like this: <h2><?php $title= "Test page"; ?><?php echo $title ?></h2> This echos the page title fine, but doesn't actually put it in the title bar. If someone could figure our why, or even figure out a mass way of doing this, or a tool I could generate and insert with I'd be so greatful I'd make you ten cups of tea. Thanks....
In Dreamweaver, click CTRL + F - Find in: Folder - Search: Source code Find: Whatever you want to find Replace: Whatever you want to replace it with -> Replace all Make backups before replacing... just in case.
Sorry, that doesn't actually answer my question. I know how to mass find and replace. My question is, if I'm typing: <h2><?php $title= "Test page"; ?><?php echo $title ?></h2> Why doesn't the title bar say 'test page' - the code in the header.php file is correct too: <title><?php echo $title ?></title> Could anyone shed some light on this? OR perhaps suggest a better way of giving all 2000 pages a unique title tag!
You mean in Dreamweaver, or your server? This code: <h2><?php $title= "Test page"; ?><?php echo $title ?></h2> PHP: ... WILL output: <h2>Test page</h2> Code (markup): If it doesn't for you, then you're something different which you're not telling us.
It sounds like you're setting the <?php $title = "TEST" ?> after the header is called. It needs to be set before it can be echo'd out. Not positive on this without seeing the code though.
I'm echoing $title, then setting title, (which works on other pages) then I'm echoing it again. Make sense?
My CODE: <?php $pagename = "Family06"; $keywords = "keywords"; $description = "Welcome..."; include 'header.php'; include 'menu.php'; ?> <h2><?php $title= "Test page"; ?><?php echo $title ; ?></h2> --------HEADER CODE: <title><?php echo $title ?></title>
Then you're doing something different there. It's not possible to echo a variable before defining it.
I was about to disagree with you and say it works on every other page, but I realised, it is 'defined' then the header is CALLED after. <?php $title = "LAW TEACHER - new chiefs will help to get CPS back on track."; include 'header.php'; <-- there see! ?> So is there anyway I can do this then... any way at all. I can't think of any solution at all now...
So what is your actual question? How to give each page a unique name/title, without having to define the title before including the header file? Maybe you could do something nasty with the output buffer. Do all your files have an include for the footer?
Yeah, that's pretty much my question. I never coded the site, and the whole thing is very SEO unfriendly... There are millions of pages and by millions I mean over 13 thousand, perhaps even more than that as site anaylizer might only check pages linked from the site map. I don't fancy going through all of those to add a title/meta - but I'd love to find a quick way if I can. Yes, it's all PHP includes.. header/menu - mainpage - footer. For now I have a conditional statement say if $title is set, echo it, if it isn't echo 'this generic one.' But I'd love a way of setting a unique and relevent title for each page, somehow using the pages H tag - <h2>title</h2> I hope that's a little clearer.
I see, I have an idea. Place this in your header file: ob_start(); PHP: ... right after the <?php tag. And place this in the last lines of your footer: <?php $buffer = ob_get_contents(); if (strpos($buffer, '<title></title>') !== false AND preg_match('~<h2>([^<]+)</h2>~', $buffer, $title)) { $buffer = str_replace('<title></title>', "<title>{$title[1]}</title>", $buffer); } ob_end_clean(); echo $buffer; ?> PHP: I didn't test it, 'cause I was too lazy to reconstruct your site logic. But give it a try...
Hey, I've just got back to work and seen your reply. Before I test it could you explain what it does to me, cus then I'll love you forever and make you lots of cups of tea and bring you cake.
ob_start() turns on output buffering. That means no output is sent to the browser until we tell PHP to. Then we read the output in a variable, and we check if there's a title in the <title> tags. If not, we check if there's a title between the <h2> tag, and if so, we put the same in the <title>... and finally output everything.
That sounds good... I'm going to give that a try thank you. Choose a cake from pimpmysnack.com and I'll send it to you in return. Honest. Hehe.
Looking at some of the code above, it appears the $title might be in the wrong place basically your <title></title> tag needs to be inside the <head></head> tag, and the head tag needs to be before the <body></body> tag. something like <html> <head> <title><?=$title;?></title> </head> <body> HTML: Hopefully that helps, obviously you'll need to set $title before you use it . Also just a note, you might want to look at the Smarty templating system - it helps with templating and makes life easier =)
The first point was already suggested, and the second falls out of question, 'cause he has too many files and doesn't want to edit them all.