Jump to content

Recommended Posts

It didnt start out as one, but it ended up as one :) and now im stuck.

 

basically, it'll see if the first character is a number, and if it is, will strip it off and continue down the string.

 

i have this function

 

function add(){

$fav=$_REQUEST['fav'];

 

$this->fixfav($fav);

 

//sql query with ($fav)

}

 

and this function

 

function fixfav($fav){

 

$testfav=substr($fav,0,1);

 

if (is_numeric($testfav)){

$fav=substr($fav,1);

$this->fixfav($fav);

}else{

return $fav;

}

 

} // end function

 

my problem is, that i can get it to do the if's proper, but it keeps returning the original value :(.

 

thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/149770-solved-first-recursive/
Share on other sites

Why not use an explode function?

 

Say something like:

 

$blah = 'foooobar';
$exp = explode('', $blah);
foreach ($exp as &$char) {
if (is_numeric($char)) {
	$char = '';
}
}
unset($char);
$blah = implode('', $exp);

 

 

Oh almost forgot, if you put an else on that loop with a break; in it, then you'll get the recursive behavior you were looking for, except that it's not really recursive in this case lol (a plus in my mind :) )

I keep getting an empty deliminator error for function explode.

 

Is it not allowing me to split at every character?

 

Im using strings like: "1new";

$exp = explode('', $fav);

foreach ($exp as $char) {
	if (is_numeric($char)) {
		$char = '';
		} else {			
			break;				
			}
	} // end foreach

$fav = implode('', $exp);

You're welcome, and apologies on the explode problem. I would have bet $5 on it working, as I could have sworn I've done that before lol.

 

The foreach loop allows the value to set by reference. Therefore, changing the value of the value changes the value of the value in the array.

 

In this case, &$char in the foreach loop allows me to set the value of $char and that change is reflected in the $exp array.

 

The alternative is using the $exp as $key => $char deal and setting $exp[$key]. The big thing is unset()ing the $char variable since any modification of that var after the foreach loop would still edit the wichever array value it points to.

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.