I'm sure this is really simple but I just can't find a way to do it:
I have a template file for a website which naturally contains:
<title>website name</title>
What I need to do is change the page title to a product name, and up until now I have simply been using str_replace to change "website name" to "product name". However, now the template file is updated often and not by myself, and the title sometimes get changed. What I'm looking for is some code to find "<title>*anything*</title>" and change it to "<title>product name</title>".
From what I can tell preg_replace is the way to go, but I've not used it before and can't figure out how to apply it here. Can anyone give me a helping hand on this one?
Thanks in advance!
Replacing an html page title
Moderator: General Moderators
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Try using a switch.
Example:
Example:
Code: Select all
<title>Company Name - <?php
switch ($product) {
case 'fish_rod': echo 'Fishing Rods';
break;
case 'moon_rock': echo 'Moon Rocks';
break;
default: echo 'A nice company we are!';
break;
}
?></title>- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I see what you're saying but I'm struggling to see why you need this.. Are you getting the document contents using file_get_contents() or fread() or similar? The RegExp would be something like:
Code: Select all
$title = 'Your product';
$new_document = preg_replace('/<title>(.|\s)*?<\/title>/i', '<title>'.$title.'</title>', $original_document);