Skip to content

Changing text on web pages – Part 2

In this issue we’ll discuss smarter ways to use the replace function plus a more advanced method to insert dynamic text.

DYNAMIC TEXT IN WEB PAGES – PART 2

In the last issue of Office for Mere Mortals we talked about the idea of updating a web site using dynamic text so you can change all the places where a word or phrase appears easily.

We covered the basic concepts, the in-built Frontpage method and simple replace options. We also talked about the limitations of the simplistic ‘find and replace all’ approach in many real world situations.

In this issue we’ll discuss smarter ways to use the replace function plus a more advanced method to insert dynamic text.


TAGGED SEARCH AND REPLACE

Web pages are made up of tags that tell your browser where and how to display elements. Tags begin and end with ‘less than’ ‘greater than’ symbols like this

. Most commonly tags are in pairs with a beginning tag plus a tag at the end prefixed with the / character. For example to change some text to bold looks like this This is Bold Text the first tags says to make something bold and the second tag tells your browser to switch off the bold option.

What makes HTML so powerful is that the tags can say anything – if your browser doesn’t recognize a particular tag it just ignores it and moves on. This is significantly different from regular programming languages where any unknown command or phrase causes the entire program to grind to a halt.

All that means you can make up your own tags to identify pieces of text and use those tags to target a find and replace command accurately. The tags can be anything you want as long as they don’t use any of the existing HTML tags.

Let’s return to the example in the last issue of an organization where you have a new chairman appointed. You can’t just replace all the instances of the old chairman’s name because he’s still around and listed in other places on the site. Using custom tags you can mark the places where the chairman’s name is used like this:

Fred Dagg

When there’s a change use find and replace across all the web pages in the source code to replace that with

Bruce Bayliss

Any places where Fred’s name is used other than as Chairman are left unchanged.

A few notes about this trick:



  • I use the ‘usr_’ prefix for all custom or ‘user defined’ tags. This makes it clear that these are special tags and makes it highly unlikely that there will be a conflict with a formatting HTML tag.
  • Try to have tags that are short, but not so short that it’s unclear what the tag is for.
  • Strictly speaking you don’t need both tags, just one will do. But for the sake of accuracy and certainly I prefer to have both a beginning and ending tag.
  • The tags will appear in the source code visible to any browser of the web site that cares to look.
  • This trick works on any type of web page.
  • And yes, it’s really an informal kind of XML tagging.

For occasional changes on small web sites this trick works well. It’s not dependant on any particular web page editor or web host.

WEB PAGE HEADER

Another option is to have a header on the web page with a list of all the variables used on the page. Then in the text itself you have a special piece of code to tell your web host to display the variable set at the top of the page. This takes a bit more setup work but gives you considerable flexibility and power later on.

To use this option you need to know what language your web host supports. Chances are your web host will support either PHP or ASP, perhaps both. Once you know that you’ll need to rename all the web pages to the correct extension – either .PHP or .ASP.

Regardless of which system you use, the basic principle is the same. When someone requests a particular web page, the server runs the code on the page and sends the results to the browser. The end user only sees the web page, not the programming code.

One disadvantage of this option is that you can’t see the final page until you post it to a web host, unless you install Internet Information Services (IIS) which is an optional part of some Windows versions plus PHP support if necessary.

Once you have this setup you can change the variables at any time, even using search and replace across multiple web pages.

We’ll cover the basics of both systems here, for more detail there are plenty of web sites that have information on either PHP or ASP.

ASP HEADER

Active Server Pages are the Microsoft preferred web page programming language. We’ll stick with the original ASP though there is now ASP.net with web pages using the .aspx extension.

Make sure the web page has an .asp file name extension (not .htm or .html).

At the top of the source for the web page you setup the variables like this:

<%

‘ Set Chairman’s name

usrChairman = “Fred Dagg”

%>

The <% and %> separate the ASP code from normal web tags. The apostrophe marks the start of a comment line.

To display the variable in the web page insert this:

<% =usrChairman %>

Wherever you want the Chairman’s name to appear.

PHP HEADER

For PHP web hosts the web pages need a .php file name.

At the top of the source for the web page you setup the variables like this:

$first_name = “Jupiter”;

$last_name = “Jones”;

?>

Note that in PHP each line must end with a semi-colon. This is a common stumbling block for beginners when their pages crash.

To display the variables:

First Name:

Or

Last Name:

 

About this author

Office 2024 - all you need to know. Facts & prices for the new Microsoft Office. Do you need it?

Microsoft Office upcoming support end date checklist.