PHP / MySQL Q: where can I get a script to scan contents of page when it loads and replace a certain value with a string of text?
October 4, 2004 6:48 PM   Subscribe

PHP / MySQL Q: Is there anywhere I can get a script that will scan the contents of a page when it loads and replace a certain value with a string of text? {more}

I want to be able to put text like this on a page:


and have the PHP replace that with

John Smith --- clickable to email

I assume it'll involve making php files for each of the contacts and then having the php drop in an include command thingamajiggy. Either that or building a mysql database and dropping in an array or some such thing. (Obviously, with my technical expertise is visibe by my mastery of coding jargon.)

posted by dobbs to Computers & Internet (9 answers total)
Obviously, with my technical expertise is visibe by my mastery of coding jargon.

As is my mastery of grammar and spelling. Ignore the word with, please. Visibe = visible.
posted by dobbs at 6:52 PM on October 4, 2004

I think that by default PHP will stream HTML as you make it, so there's no chance to replace strings.

But you can turn on output buffering so that all the print statements and such are stored up and then sent as one blob.

So at the top of your PHP turn on output buffering, then do your page as per normal, and then at the end read the output buffer with ob_get_contents(), do a str_replace() on it, turn off the buffer and print the result.
posted by holloway at 7:01 PM on October 4, 2004

You can do this, but why on earth would you? You're not going to like the answer, very much, I can tell you that. Could you explain what you're trying to do -- there might be a more elegant solution.
posted by Civil_Disobedient at 7:10 PM on October 4, 2004

I'm not sure I am getting what you're asking exactly, but I handle a similar problem in my photoblog script by doing an str_replace on the entire document using an array. The script declares all the variables (in your case, I'd load them from MySQL) as a keyed array

and then replaces the keys with the values in the template file (which I load as one string). The script's only output statement, then, looks like this:
echo str_replace(array_keys($tags),$tags,file_get_contents("template.php"));
(Thus this becomes this.) I'd load in the variables from the MySQL database.

If you're looking for templating on a general scale, try smarty. I hadn't heard about it until this weekend, but it was suggested to me as an alternate to my current template system.
One of Smartys primary design goals is to facilitate the separation of application code from presentation. Typically, the application code contains the business logic of your application, written and maintained in PHP code. This code is maintained by programmers. The presentation is the way your content is presented to the end user, which is written and maintained in template files. The templates are maintained by template designers.
posted by rafter at 7:15 PM on October 4, 2004

Thanks for your answers. Rafter, I think that's what I'm after. Thanks.

Halloway, I need to do it because I have a client who has many employees and every time they post a press release they have to look up the contact info of the person who should be contacted for more info. If I can build a database of the various contacts, they only have to drop the first inital and name into the contact field and the script will do the rest.
posted by dobbs at 7:34 PM on October 4, 2004

I think you mean C_D, but yeah, it sounds like a templating engine like Smarty would be best.
posted by holloway at 7:51 PM on October 4, 2004

Template system might be a bit overkill for just this scenario. PHP has some built-in text replacement functions like ereg that'll do the trick.
posted by billsaysthis at 9:13 PM on October 4, 2004

Ah, so you don't really have to parse the entire file -- you're parsing a variable. I'm much happier to hear that. I thought you were talking about having to parse through an entire web page, tags and all, which would be slow and hell and ugly an sin.
posted by Civil_Disobedient at 12:09 AM on October 5, 2004

If it's not obvious to you how to do it in PHP, you might want to stick with the simplest possible solution, which is probably SSI.

A program that outputs the html for each contact: <!--#include virtual="/cgi-bin/ JSmith" -->

Or each contact with its own html file: <!--#include virtual="/contacts/JSmith" -->
posted by sfenders at 9:18 AM on October 5, 2004

« Older I've just moved; should I register to vote in the...   |   We need to talk. Newer »
This thread is closed to new comments.