...

View Full Version : str use limiter



Digicoder
12-28-2007, 03:29 AM
I'm running into a bit of trouble with this code I recently made, in theory it would have worked fine.

I need help on how I can get the following done:
I need to match a changing string and then replace it based on how many times it was previously replaced.
I've tried many variations of this, here is my most recent:



$i = 0;
$num = 0;
while($num != 600)
{
if(preg_match('/\[img\](.+?)\[\/img\]/is', $bb2html))
{
if($i <= 5 ){
$bb2html = preg_replace('/\[img\](.+?)\[\/img\]/is', '<img alt="an image" src="\\1" />', $bb2html);
$i++;
echo $i.'img let thru';
}
if($i > 5){
$bb2html = preg_replace('/\[img\](.+?)\[\/img\]/is', '<a href="\\1">\\1</a>', $bb2html);
$i++;
echo $i.'img detered';
}
}
else
{
$num = 600
}
}


Well the while and preg_match cases seem to work fine but I'm not seeing any filtered results based on how many times that it has been matched.
I can't think of any other ways to do it, I need a little extra brain power here.

Thanks all for the help.

oesxyl
12-28-2007, 04:50 AM
$i = 0;
while(preg_match('/\[img\](.+?)\[\/img\]/is', $bb2html)){
if($i <= 5 ){
$bb2html = preg_replace('/\[img\](.+?)\[\/img\]/is', '<img alt="an image" src="\\1" />', $bb2html, 1);
$i++;
echo $i.'img let thru';
}elseif($i > 5){
$bb2html = preg_replace('/\[img\](.+?)\[\/img\]/is', '<a href="\\1">\\1</a>', $bb2html);
$i++;
echo $i.'img detered';
}
}
}


I add a 1 as "limit" argument to first preg_replace, transform second if in elseif, and replace the while condition, last ones for beauty, :)

best regards

Digicoder
12-28-2007, 05:31 AM
Haha, thanks, You've made me feel like a total fool.
That one was obvious enough... I can't believe I over looked that one.

This means its either time for sleep, or more coffee.
Thanks again :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum