Setting TITLE using text from H1 header in Dreamweaver

Here's a little code snippet that might make your life easier. If you wanted to set your page title to include the text from your header (in my case, the H1 element) you can use this search and replace. Pop open the Find dialog and select Source Code from the Search drop down. Enter the following into the Find field:

<title>(.)*</title>((.|\n)*)<h1>(.)+</h1>

Luckily, Dreamweaver's regular expression support isn't greedy so this will find your title tag your h1 tag and everything in between. All we need to do now is rearrange things. Enter the following in the replace field.

<title>$4 - Site Name</title>$2<h1>$4</h1>

The dollar sign and number combo identify which set of parentheses to take. Therefore, we replace our match with our new title tag to include the third parentheses (our heading) along with the site name. We then put in all the content between the title and the heading with $2. Finally, we tack on our heading as is.

Once things are ready to go, make sure Use regular expression is checked and then click Find or Replace as you need to.

I can't stress this enough. Before doing ANY site wide search and replace, ALWAYS make a backup first. Trust me on this one!

The only downfall to this method is that any tags that are present in the heading (line breaks, bolds, italics, etc) will suddenly appear in the title. Dreamweaver to the rescue again.

To clean up any tags that appear in the header, open up the Find dialog again and then choose Specific Tag from the Search dropdown. Select [any tag] from the dropdown beside it.

Select Inside Tag from the dropdown with the [+][-] beside it and then choose title from the dropdown beside that. Select Strip Tag from the Action dropdown. Click Replace All and you're good to go.

Published January 22, 2005 · Updated September 17, 2005
Categorized as Dreamweaver
Short URL: https://snook.ca/s/317

Conversation

2 Comments · RSS feed
Jehiah said on January 22, 2005

A nice creative solution that i'll have to give a try. [kudos] It has anoyed me more than once that those two items always seem to be out of sync. I have played around with logging the two pieces of information for every page so I could see how bad it really is; but this solves the problem. It also sounds like something i'll have to add to my stripformat.pl script.

Amit said on May 19, 2006

I tried to use the above RE to replace the title with h1 but it replaces the title and h1 with "<" your help would be highly appreciated.

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.

Want to learn about scaling CSS for large projects?

I'm available for full and half-day workshops on scalable CSS architecture. I can provide on-site training for your team. Interested?
Get in touch.