Jump to content

my ++is not incrementing my variable


Monkuar

Recommended Posts

Okay, for my bbcode parser, I run through [img tags with:

 

$text = preg_replace( "#\[img\](.+?)\[/img\]#ie"                             , "\\regex_check_image('\\1')"          , $text );

 

It goes to my function regex_check_image:

 

function regex_check_image($url="") {

        if (!$url) return;

        $url = trim($url);

        $default = "[img=".$url."]";
//-- mod_sec_update_131 begin
        $default = "[img=".str_replace( '[', '&# 091;', $url )."]";
//-- mod_sec_update_131 end
        $image_count++;
	echo $image_count;
	exit;

            if ($image_count > 6)
            {
                message('You have posted to many Images');
            exit;
            }
            if (preg_match( "/[?&;\<\[]/", $url))
            {
                message('No Dynamic Images');
            exit;
            }
            if (preg_match( "/javascript(\:|\s)/i", $url ))
            {
                 message('No Dynamic Images');
            exit;
            }

        // Is it a legitimate image?

        if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
            message('No Dynamic Images');
            exit;
        }

        // If we are still here....

        $url = str_replace( " ", "%20", $url );

        return "<img src='$url' border='0' />";
    }

 

Everything works fine, but for some reason,

   $image_count++;
	echo $image_count;
	exit;

 

if I have like 20 [img tags with images in them, I try to debug it, and echo it out, and it only shows "1" it should show how many [img tags I have, so I can echo out if somone is trying to spam images with the [img tag, any help?

Link to comment
Share on other sites

The command exit; terminates the script. You are incrementing the variable, echoing the variable to the screen, then stopping the whole script. It looks like you need to get rid of all your exit; commands.

 

well those exit's are under if functions so it should be fine?

 

I see what you mean tho

 

I have moved the code underneath my function

 

function regex_check_image($url="") {

        if (!$url) return;

        $url = trim($url);

        $default = "[img=".$url."]";
//-- mod_sec_update_131 begin
        $default = "[img=".str_replace( '[', '&# 091;', $url )."]";
//-- mod_sec_update_131 end
$counter++;
            if (preg_match( "/[?&;\<\[]/", $url))
            {
                message('No Dynamic Images');
            exit;
            }
            if (preg_match( "/javascript(\:|\s)/i", $url ))
            {
                 message('No Dynamic Images');
            exit;
            }

        // Is it a legitimate image?

        if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
            message('No Dynamic Images');
            exit;
        }

        // If we are still here....

        $url = str_replace( " ", "%20", $url );

        return "<img src='$url' border='0' />";
    }
if ($counter > 6)
            {
                message('You have posted to many Images');
            exit;
            }

 

and it doesn't seem to get my variable $counter .... weird any ideas guys?

Link to comment
Share on other sites

I don't see where you are initializing $counter.  If it is outside the function, then this code won't work, since this variable inside the function will have its own scope within the function. Try adding:

 

global $counter;

 

at the top of your function to see if that works.  But it will only work if you are declaring $counter outside your function (and outside any other functions).

Link to comment
Share on other sites

I don't see where you are initializing $counter.  If it is outside the function, then this code won't work, since this variable inside the function will have its own scope within the function. Try adding:

 

global $counter;

 

at the top of your function to see if that works.  But it will only work if you are declaring $counter outside your function (and outside any other functions).

 

global $counter;
function regex_check_image($url="") {

        if (!$url) return;

        $url = trim($url);

        $default = "[img=".$url."]";
//-- mod_sec_update_131 begin
        $default = "[img=".str_replace( '[', '&# 091;', $url )."]";
//-- mod_sec_update_131 end


       $counter++;
        // Make sure we've not overriden the set image # limit

   
            if ($counter > 10)
            {
                message('You have posted to many Images');
            exit;
            }
            if (preg_match( "/[?&;\<\[]/", $url))
            {
                message('No Dynamic Images');
            exit;
            }
            if (preg_match( "/javascript(\:|\s)/i", $url ))
            {
                 message('No Dynamic Images');
            exit;
            }

        // Is it a legitimate image?

        if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
            message('No Dynamic Images');
            exit;
        }

        // If we are still here....

        $url = str_replace( " ", "%20", $url );

        return "<img src='$url' border='0' />";
    }
echo $counter;
exit;

doesn't seem to echo out anything now, not even "1" and im copy/pasting like 30 tags

 

hmmm

Link to comment
Share on other sites

Put it inside the function:

 

function regex_check_image($url="") {
global $counter;

...

 

Where do you initialize the $counter variable?

 

You said put it ontop, lol my bad though

 

I initialize it using

 

"  ++$counter; "

 

++ auto increments right? or $counter++, what's the difference?

 

I Still put it underneath and nothing is being called, it seems like I can only echo out the stuff if it's inside the function,

 

 

function regex_check_image($url="") {
global $counter;

        if (!$url) return;

        $url = trim($url);

        $default = "[img=".$url."]";
//-- mod_sec_update_131 begin
        $default = "[img=".str_replace( '[', '&# 091;', $url )."]";
//-- mod_sec_update_131 end


       ++$counter;
        // Make sure we've not overriden the set image # limit

   
            if ($counter > 10)
            {
                message('You have posted to many Images');
            exit;
            }
            if (preg_match( "/[?&;\<\[]/", $url))
            {
                message('No Dynamic Images');
            exit;
            }
            if (preg_match( "/javascript(\:|\s)/i", $url ))
            {
                 message('No Dynamic Images');
            exit;
            }

        // Is it a legitimate image?

        if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
            message('No Dynamic Images');
            exit;
        }

        // If we are still here....

        $url = str_replace( " ", "%20", $url );

        return "<img src='$url' border='0' />";
    }
echo $counter;
exit;

 

still echo's out nothing, hmm

I am so close I think :o

 

Even if I do echo it out inside the function itonly displays "1", not 20 or 30, or how many [img tags I have, prob because im exiting the script? But still the if functions if > 10 doesn't work anyway, something is wrong it's not looping through them, but the function does work and I can use multiple [img tags on my forums

 

this is so weird

Link to comment
Share on other sites

Is it possible for you to post your entire code? If it's too long, then at least post the code around where the regex_check_image function is called. I guess I'm not fully understanding how your code is supposed to work.  But basically, you want to initialize your variable, which sets a beginning value.  It's usually done like this:

 

$counter = 1;

 

If you're using the variable as a counter (which you are in this case), you want to initialize the variable OUTSIDE the code that increments the variable.  Otherwise it will set the $counter back to 1 every time it runs the loop (or whatever). If you are (or will) initialize your variable outside of a function, you need to globalize it inside the function (though this is frowned upon, you should really learn about function scope and passing variables or possibly using static variables inside a class).

 

I know this is a long answer, but I may be able to help you if you can post more of your code and explain in more detail what you are trying to do.

Link to comment
Share on other sites

Is it possible for you to post your entire code? If it's too long, then at least post the code around where the regex_check_image function is called. I guess I'm not fully understanding how your code is supposed to work.  But basically, you want to initialize your variable, which sets a beginning value.  It's usually done like this:

 

$counter = 1;

 

If you're using the variable as a counter (which you are in this case), you want to initialize the variable OUTSIDE the code that increments the variable.  Otherwise it will set the $counter back to 1 every time it runs the loop (or whatever). If you are (or will) initialize your variable outside of a function, you need to globalize it inside the function (though this is frowned upon, you should really learn about function scope and passing variables or possibly using static variables inside a class).

 

I know this is a long answer, but I may be able to help you if you can post more of your code and explain in more detail what you are trying to do.

 

Yes, I will!, Thanks for doing this

 

 

This is my parse_message function i made:

 

http://pastebin.com/enpCdfXN

 

 

 

(I have stripped regular <b> bbcode tags for space, but that is literally all of it.

 

I use this and wrap the parse_message around people's Post before they get entered into the Database. so it converts the img tag u see here (Which does work, just not the counter issue we have now)

 

Here is the regex_check_image function

 

 

function regex_check_image($url="") {
global $counter;

        if (!$url) return;

        $url = trim($url);

        $default = "[img=".$url."]";
//-- mod_sec_update_131 begin
        $default = "[img=".str_replace( '[', '&# 091;', $url )."]";
//-- mod_sec_update_131 end


       ++$counter;
        // Make sure we've not overriden the set image # limit

   
            if ($counter > 10)
            {
                message('You have posted to many Images');
            exit;
            }
            if (preg_match( "/[?&;\<\[]/", $url))
            {
                message('No Dynamic Images');
            exit;
            }
            if (preg_match( "/javascript(\:|\s)/i", $url ))
            {
                 message('No Dynamic Images');
            exit;
            }

        // Is it a legitimate image?

        if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
            message('No Dynamic Images');
            exit;
        }

        // If we are still here....

        $url = str_replace( " ", "%20", $url );

        return "<img src='$url' border='0' />";
    }

 

 

Then right below that I have to check $counter

 

	echo $counter;
exit;

if ($counter > 6)
            {
                message('You have posted to many Images');
            exit;
            }

 

which doesn't work either Even if I use ++$counter; or $counter++;

 

And that is literally my whole .php script man

 

 

Link to comment
Share on other sites

That's an odd place to call a function from. Does it work?

 

Also, in order to increment a counter properly, you need to be running past your $counter++ with some kind of loop (for/next, foreach, while, do/while, etc.). From where are you looping past your counter?

 

Isn't it already being looped because it has to parse each individual [img tag? The tag works fine and returns my image properly..?

 

Sorry I am pretty dam lost as u can tell.

 

But since it is being looped to do that through each [img tag, that's why I think the $counter is inside one right?

Link to comment
Share on other sites

Loops don't occur automatically. You need to write code that will loop through things. The most common functions that loop are: for/next; foreach; while; do/while;

 

These are code blocks that you can put counters inside of. For example, a typical foreach function looks like this:

 

$imageArray = array();  // create some kind of array.
// do something here to load the array up with information, such as the URLs of all of the images

foreach($imageArray as $key=>$value){   // this will loop through each element in the array, then continue outside code block when done

    // do whatever it is you're doing with EACH image

}

 

Or if you wanted to use a counter, you might use a while() or a for/next:

 

$counter = 1;
while($counter<6){  // this code will run as long as the condition is met ($counter<6). when $counter = 7, the block will end
   // do whatever you're doing with the images
$counter++;
}

 

or

 

for($i=0;$i<6;$i++) {  // the counter is in this line
      // this code will run until the middle condition is met above ($i<6), then the block will stop
}

 

I don't see any looping functions in your code. If I missed something, let me know and I'll look again.

Link to comment
Share on other sites

function regex_check_image($url="") {
$counter = 1;
while($counter<6){
$counter++;
if (!$url) return;
        $url = trim($url);

        $default = "[img=".$url."]";
        $default = "[img=".str_replace( '[', '&# 091;', $url )."]";
            if ($counter > 10)
            {
                message('You have posted to many Images');
            exit;
            }
            if (preg_match( "/[?&;\<\[]/", $url))
            {
                message('No Dynamic Images');
            exit;
            }
            if (preg_match( "/javascript(\:|\s)/i", $url ))
            {
                 message('No Dynamic Images');
            exit;
            }
        if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
            message('No Dynamic Images');
            exit;
        }
        $url = str_replace( " ", "%20", $url );
echo $counter;
exit;
}
return "<img src='$url' border='0' />";
    }

 

Okay, I took your code and tried to get it to work.

 

Now it echo's "2" Any idea? am I getting closer?

Link to comment
Share on other sites

This thread is pretty funny. The exit's, as original stated, kill the script. You want to use break;  which will break out of the loop.

 

So yea, no more exit's, use break instead. Again exit KILLs the script. Nothing gets done after that point. Break will just break the loop and continue with the rest of the code.

Link to comment
Share on other sites

This thread is pretty funny. The exit's, as original stated, kill the script. You want to use break;  which will break out of the loop.

 

So yea, no more exit's, use break instead. Again exit KILLs the script. Nothing gets done after that point. Break will just break the loop and continue with the rest of the code.

 

:'( :'( :'( :'(

 

Premiso, how am I supposed to read my echo's then when the script finishes?

Link to comment
Share on other sites

I am not exactly sure where you are going with this, so this may not be correct at all but here you go: Oh this assumes that "message" is actually a function that does something as well.

 

function regex_check_image($url="", &$counter = 1) {
while($counter<6){
	$counter++;
	 if (!$url) return;

	$url = trim($url);

	$default = "[img=http://".$url."]";
	$default = "[img=http://".str_replace( '[', '&# 091;', $url )."]";
	if ($counter > 10)
	{
		message('You have posted to many Images');
		break;
	}
	if (preg_match( "/[?&;\<\[]/", $url))
	{
		message('No Dynamic Images');
		break;
	}
	if (preg_match( "/javascript(\:|\s)/i", $url ))
	{
		message('No Dynamic Images');
		break;
	}
	if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
		message('No Dynamic Images');
		break;
	}
	$url = str_replace( " ", "%20", $url );
}

return "<img src='$url' border='0' />";
}

// usage : $counter will contain how many times it was looped.
$imgHtml = regex_check_image($url, $counter);

echo "Checked $counter images!";

Link to comment
Share on other sites

OK. We'll go through some of this line-by-line:

 

$counter = 1;

This initializes the variable $counter and sets its value to 1;

while($counter<6){

Now we are creating a loop that SHOULD run 5 times ($counter is 1, loop as long as $counter < 6, but not including 6, so 1-5)

$counter++;

This immediately sets $counter to 2. Traditionally, this should go at the END of the loop, not the beginning.

 

Then we have the code in the middle. This code is set up to run over and over, not changing through each loop. This is a problem, because you don't want to run code against the same file every time (right?), but instead run through a list of files. This is why programmers usually use the counter (or some other variable that is set up to change during each loop iteration) in the loop. For instance:

 

$counter =1;
while($counter<10){

$myURL = $url."/images/"."image{$counter}.jpg";
// now do something with the images.

$counter++;
}

 

Notice how I included the $counter variable in my code? So on my system, my images would be named with the convention "image0.jpg", "image1.jpg", "image2.jpg", etc... so my loop would load up everything correctly by using my counter. You don't have to use this way to loop through the images. You can also load up an array and use a foreach or whatever so you don't have to use the counter in this way.

 

Finally, the end of your function has:

 

exit;

Which will terminate the entire application and not let the loop continue. You should remove the exit; and put the $counter++; at the end of the function (but still inside the function block). So your code is terminating before the counter can be moved past 2, which is why it is giving you that number on your echo.

 

I hope this makes a little more sense.

 

I'm out for now. Post your findings and I will check it later or tomorrow or next week.

 

Cheers.

Link to comment
Share on other sites

I am not exactly sure where you are going with this, so this may not be correct at all but here you go: Oh this assumes that "message" is actually a function that does something as well.

 

function regex_check_image($url="", &$counter = 1) {
while($counter<6){
	$counter++;
	 if (!$url) return;

	$url = trim($url);

	$default = "[img=http://".$url."]";
	$default = "[img=http://".str_replace( '[', '&# 091;', $url )."]";
	if ($counter > 10)
	{
		message('You have posted to many Images');
		break;
	}
	if (preg_match( "/[?&;\<\[]/", $url))
	{
		message('No Dynamic Images');
		break;
	}
	if (preg_match( "/javascript(\:|\s)/i", $url ))
	{
		message('No Dynamic Images');
		break;
	}
	if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
		message('No Dynamic Images');
		break;
	}
	$url = str_replace( " ", "%20", $url );
}

return "<img src='$url' border='0' />";
}

// usage : $counter will contain how many times it was looped.
$imgHtml = regex_check_image($url, $counter);

echo "Checked $counter images!";

 

Hey,

 

function regex_check_image($url="", &$counter = 1) {
while($counter<6){
	$counter++;
	 if (!$url) return;

	$url = trim($url);

	$default = "[img=http://".$url."]";
	$default = "[img=http://".str_replace( '[', '&#38;# 091;', $url )."]";
	if ($counter > 10)
	{
		message('You have posted to many Images');
		break;
	}
	if (preg_match( "/[?&;\<\[]/", $url))
	{
		message('No Dynamic Images');
		break;
	}
	if (preg_match( "/javascript(\:|\s)/i", $url ))
	{
		message('No Dynamic Images');
		break;
	}
	if (!preg_match( "/^(http|https|ftp):\/\//i", $url )) {
		message('No Dynamic Images');
		break;
	}
	$url = str_replace( " ", "%20", $url );
}

return "<img src='$url' border='0' />";
}

// usage : $counter will contain how many times it was looped.
$imgHtml = regex_check_image($url, $counter);

echo "Checked $counter images!";
exit;

 

Still only displays

 

"Checked 1 Image" when im copy/pasting this bbcode

 

[img=http://fluxbb.org/files/images/logo.png]
[img=http://fluxbb.org/files/images/logo.png]
[img=http://fluxbb.org/files/images/logo.png]
[img=http://fluxbb.org/files/images/logo.png]
[img=http://fluxbb.org/files/images/logo.png]

 

which should display "Checked 5 Images" Right?

 

And It's not because of the exit, because with your code i put

 

$imgHtml = regex_check_image($url, $counter);
if ($counter > 4){
message("You have posted to many images, you're only allowed 5");
}

 

And it's still not giving the error, I could replace message with echo too, it's pretty much the same, message is just my global function to display errors, but yea, either way still not workign any idea?

 

 

@ batwimp, im having same issue with ur code right now to as I posted above

 

thanks if u can help

 

I am absolutely stumped

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.