Re: Small problem with function..
| From: | Sylwester Zarębski | Date: | Tue, 13 Jun 2000 07:25:44 +0000 |
| Subject: | Re: Small problem with function.. | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-1694@lists.php.net to get a copy of this message | ||
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 |