RE: [PHP-GENERAL] Small problem with function..

From: Date: Tue, 13 Jun 2000 12:45:50 +0000
Subject: RE: [PHP-GENERAL] Small problem with function..
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-1708@lists.php.net to get a copy of this message
The problem is some of your "pos" variables do not have a "$" in front of them. "pos+1" evaluates to "1", so the character never gets deleted from "$string", and the while loop is an infinite loop that keep building a bigger string. > -----Original Message----- > From: Sylwester Zarębski [mailto:sylwek@isp.net.pl] > Sent: Tuesday, June 13, 2000 2:26 AM > To: PHP general list > Subject: Re: [PHP-GENERAL] Small problem with function.. > > > Szii wrote: > > > > Sylwester Zarębski wrote: > > > > > > I have problem with small function. This function doesn't work > > >as expected, and consume all available memory and kills apache > > >server. Here goes my function: > > > > > >function strdelc ( $string, $chars ) { > > > $chars_len = strlen ( $chars ); > > > for ( $cnt = 0; $cnt < $chars_len; ++$cnt ) { > > > $char = substr ( $chars, $cnt, 1 ); > > > while ( ( $pos = strpos ( $string, $char ) ) || is_int ( $pos ) ) { > > > if ( $pos == 0 ) { > > > $string = substr ( $string, pos + 1 ); > > > } elseif ( $pos < strlen ( $string ) - 1 ) { > > > $string = substr ( $string, 0, $pos - 1 ) . substr > $string, pos > > >+ 1 ); > > > } else { > > > $string = substr ( $string, 0, $pos - 1 ); > > > } > > > } > > > } > > > return ( $string ); > > >} > > > > > Check your "while()" conditions. Are you sure you want to be doing > > an assignment to $pos from strpos( $string, $char)? The = will > > ALWAYS evaluate to true (if strpos() doesn't find the string > it'll return > > FALSE and set the $pos = FALSE but the actual ASSIGNMENT works > > successfully and therefore the statement is TRUE, and you'll hit your > > infinite loop condition. > > Hmm.. i didn't know this works like that :-(( > I didn't find any word for that 'behaviour' in manual. > I thought this works rather like C. > But i think you're wrong.. > I observe that this loop only happens, if $string contains one of $chars.. > Then your statement about _true_assignnment_ is false.. > And then i suspect some weird string operations.. > I repeat: > this only happens if $string contains some (one ??) of $chars > > Please.. some other explanation ??? > > -- > pozdrawiam.. > > ## ## | Sylwester Zarębski - ISP Group | > #### ## | e-mail: sylwek@isp.net.pl | > ## ## ## | ICQ uin: #45780888 | > ## #### ## | Administrator ISP.NET.PL | > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#1708) next »