Page 1 of 1
Buffer Filtering
Posted: Tue Oct 28, 2003 3:10 pm
by Gen-ik
Ok.. I've got a webpage which get's fully buffered before being dumped to the browser. Before the page gets dumped I run through the buffer to add little bits here and there to the page... which is working fine.
Here's the question..
I'm trying to find the easiest way of asigning an id to each link in the page, link one gets id 1, link two gets id 2, and so on.
I'm thinking I can probably get away with running a strpos loop on the buffer ( I'm searching for <a href ) and then do a str_replace on each link that get's found...... but was wondering if there is a quicker way?
Thanks.
Posted: Tue Oct 28, 2003 6:30 pm
by JAM
../index.php?id=1 (etc.) you mean?
If so, how can you be sure that the links are in the correct order (if any).
I have an idea, but just want to make sure I fully understood it, as it's usually a good idea before posting an 'answer'. =)
Posted: Tue Oct 28, 2003 7:57 pm
by Gen-ik
Nope not quite... the id will be place on the <a> tag..... ie
<a id="link1" href="somewhere.or.something">A Link</a>
I've got it working using a combination of
strpos and
substr_replace but I was kinda hoping it could be done faster using an ereg of some sort.
The problem is I still can't get my brain on the same wavelength as the ereg syntax.... and haven't found any good, clear, resource or tutorials yet

Posted: Tue Oct 28, 2003 8:12 pm
by JAM
Ah... Perhaps this might help. It's not at all what you require, but others might take the usage of it.
Once i did something similiar.
Code: Select all
// $addy = http://example.com/
// $info = "this is a link..."
function make_link($addy,$info) {
// code here
}
If you printout links using a function, you might be able to add
'id=link' + &static $integer that you change +1 each time running the function.
Of course, preg_replace/ereg_replace sound like a more obvious approach, but I have the same issues with that as you, so I thought I could mention something else. I'm in denial that preg/regexps exists at this minute...
Posted: Tue Oct 28, 2003 8:21 pm
by Gen-ik
This is the sort of thing I'm using at the moment.... this will find all of the <font class="huge"> tags in the page and add a random background position for each one, giving the site a nice dynamic feel.
It's pretty much the same thing I use to put ID tags on links but I'm presume preg or ereg would be faster (and smarter).
Code: Select all
<?
while($pos!==false)
{
$str = "<font class="huge">";
$pos = strpos($buffer, $str, $pos);
if($pos!==false)
{
$x = rand(0,400);
$y = rand(0,100);
$rep = "<font class="huge" style="background-position:{$x} {$y}">";
$buffer = substr_replace($buffer, $rep, $pos, strlen($str));
$pos = $pos + strlen($rep);
}
}
?>