perkiset

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:

<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)


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...


<?

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

nutballs

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.

perkiset

thanks man... glad it helps.

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

JasonD

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://

php

 htmltemplate.sourceforge

.net

 /

nutballs

cool, looks basically like the same principle. I personally prefer mine since its less characters Applause 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


Perkiset's Place Home   Politics @ Perkiset's