The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 10:23:37 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Dynamic PHP site take 2: Replacing tokens  (Read 2724 times)
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« on: September 14, 2007, 01:19:37 PM »

NBs posed a question to me about replacing different tokens in a text block, but he called them "smart tokens"
which I take to mean tokens that have some form of specialized function or parameters attached to them. So
I'm posting this as an example of how you'd collect them and do something with them:

Consider this input text:
Code:
<html>
<head>
##TITLE##
##KEYWORDS##
</head>
<body>
##HEADER##

##SEARCHRESULTS-5##

##PRODUCTS-10##

##FOOTER##
</body></html>

The deal here is that we don't really know what tags are in the body text, and some, like SEARCHRESULTS-5 don't fit
a standard search-replace mechanism because he might have SEARCHRESULTS-7 or something. Note that I'm using the ##TOKEN##
notation simply as a type of pseudo-standard here - it has no PHP meaning. Here's an approach to handing it:

(assume that $buff contains the text from above)

Code:
preg_match_all('/##([A-Z0-9\-]+)##/', $buff, $matches);

At this point, the array $matches would look like this:
(0)
   (0)##TITLE##
   (1)TITLE
(1)
   (0)##KEYWORDS##
   (1)KEYWORDS
(2)
   (0)##HEADER##
   (1)HEADER
(3)
   (0)##SEARCHRESULTS-5##
   (1)SEARCHRESULTS-5
   
... and so forth. The interesting thing about this function is being able to specify a pattern to match, but then also a parenthesized "collection" parameter
that goes into the $matches array. preg_match_all is much more powerful than just this, but that's a fine example.

The next step is to iterate through the main array to get the items that need to be replaced and do the work. For this I will suggest an analysis section, followed by a single search and replace function. This is the fastest way to go, because the fewer times you can str_replace the better. I'll also be just a bit more verbose than I normally would here to make the code clear. I also make the assumption that only a token with parameters would have a dash in it - you can obviouly extend the thinking - I just wanted to make this very clean. Please note that I'm typing right here and not bug or syntax checking...

Code:
<?php

foreach($matches as $item)
{
$keyword $item[1];
$param '';
if (strpos('-'$keyword))
{
// There is a parameter!
// Remember, the zeroth will be the entire match, 1th and 2th will be just the collected () text...
preg_match('/([A-Z0-9]+)-([A-Z0-9]+)/'$keyword$plist);
$keyword $plist[1];
$param $plist[2];
}

$searchArr = array();
$replaceArr = array();
switch($keyword)
{
case 'TITLE':
$searchArr[] = '##TITLE##';
$replaceArr[] = 'The Really Big Site';
break;
case 'KEYWORDS':
$searchArr[] = '##KEYWORDS##';
$replaceArr[] = 'Mortgage, loans, viagra, oxycontin, jessica simpson';
break;
case 'HEADER':
$searchArr[] = '##HEADER##';
$replaceArr[] = file_get_contents("$themeDir/header.html");
break;
case 'SEARCHRESULTS':
$searchArr[] = '##SEARCHRESULTS##';
$replaceArr[] = buildSearchResults($param);
break;
case 'PRODUCTS':
$searchArr[] = '##PRODUCTS##';
$replaceArr[] = buildProductGallery($param);
break;
case 'FOOTER':
$searchArr[] = '##FOOTER##';
$replaceArr[] = file_get_contents("$themeDir/footer.html");
break;
}
}

// Here comes the one-pass string work:
echo str_replace($searchArr$replaceArr$buff);
?>


I know I know, about a bazillion ways you can do that... this is just a gear spinner.

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #1 on: September 14, 2007, 02:59:33 PM »

bingo, exactly what i meant.

to expand, you can do this

##QUICKGRID_5-10##
if you wanted to make a 5x10 grid.

then as you iterate through each token match like you did in the example, you test for _ and if it exists, split the stuff up and deal with it, otherwise its just a plain token. Your example is a perfect starting point.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: September 14, 2007, 03:44:36 PM »

thanks man... glad it helps.

Post away with the qqs... makes great stuff for helper threads.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
JasonD
Expert
****
Offline Offline

Posts: 100


View Profile
« Reply #3 on: September 17, 2007, 08:45:52 AM »

I have always used HTML Template - http://html-template.sourceforge.net/ a Perl module for templating as it is extremely simple but also very powerful.

It has also been ported to PHP and if half the power has been retained it is a glory to work with - http://phphtmltemplate.sourceforge.net/
Logged
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #4 on: September 17, 2007, 03:06:56 PM »

cool, looks basically like the same principle. I personally prefer mine since its less characters Smiley But i am going to look at system a little more closely, since it might have some methods I didnt think of. thanks for the pointer Jason
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!