Replacing a short bit of text in PHP is easy. You just use str_replace, as in $bobo = str_replace("foo","bar",$bobo). That will replace all instances of "foo" with "bar" in the string $bobo.

But, on a recent contract, I had a client's site where it only worked properly in Microsoft Internet Explorer and part of my task was to get it to work similarly in all browsers. One of the things they had done was use ALT tags, so that when you rolled your mouse over a graphic, the alt text would pop up as a tool tip. The problem is that this is a non-standard behavior. Only Microsoft Internet Explorer pops up alt tags. So how to get the alt text to pop up in all browsers? Javascript, of course.

I'm not going to go into the Javascript. There are many different ways to do a pop-up tooltip. The trick, though, was to get all those alt texts fed to the tooltip script. Now, there may have been a way in the DOM to get the element's alt text and feed it to the script, but I wasn't sure and didn't have a whole lot of time to hunt it down. Instead I wrote up a quickie PHP script to handle it. This is not a CGI, but a script I ran from the command line. And you don't need to be running from the Linux or Mac command line. You can install PHP on Windows XP or Vista and run it from the DOS command line.

The first part of the program recursed the directory with all of the HTML files, and each time it found an HTML file, it fed it to a function in the script that read it, altered it, and saved it. I'll write a post on how to do that soon. For now, I'm just going to get into the alteration.

Let's assume I had a line of HTML like this...

<img src="fred.jpg" alt="This is a picture of Republican presidential candidate, Fred Thompson" height=300 width=190 border=0>

As I read through the file, line by line (each line being $line), I applied the following bit of code...

if(preg_match("/alt=\"[^\"]+\"/i",$line){
   $newline = preg_replace("/alt=\"([^\"]+)\"/i","alt=\"\\1\" onMouseOver='toolTip(\"\\1\")'",$line);
} else {
   $newline = $line;
}

That will turn...

<img src="fred.jpg" alt="This is a picture of Republican presidential candidate, Fred Thompson" height=300 width=190 border=0>

... into ...

<img src="fred.jpg" alt="This is a picture of Republican presidential candidate, Fred Thompson" onMouseOver='toolTip("This is a picture of Republican presidential candidate, Fred Thompson")' height=300 width=190 border=0>

Now, how did that work? What preg_replace does is store all the parts of your regular expression that are within parentheses in a series of numbered strings. So alt=\"([^\"]+)\" set everything inside the quotes after alt= as a string which could be re-used. So we replaced alt=\"([^\"]+)\" with itself (alt=\"\\1\") plus a call to the JavaScript tooltip function using the same text as in the ALT tag (onMouseOver='toolTip(\"\\1\")').

You could also do parentheses within parentheses, capturing a larger piece of text and then capturing a smaller subset of that piece of text. The system counts the parentheses from left to right. For simplification, I'll demonstrate this with a sentence and how the pieces would break out.

Fred (Thompson is a (Republican candidate for (President)) of the) United (States)

That would break out as...
1: Thompson is a Republican candidate for President of the
2: Republican candidate for President
3: President
4: States

And that's how that works. I'll do a post soon on how to go through a directory and use it on all files.

Tags: ,
Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>