seventheyejosh Posted March 17, 2009 Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149770-solved-first-recursive/ Share on other sites More sharing options...
Floydian Posted March 17, 2009 Share Posted March 17, 2009 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/149770-solved-first-recursive/#findComment-786473 Share on other sites More sharing options...
seventheyejosh Posted March 17, 2009 Author Share Posted March 17, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/149770-solved-first-recursive/#findComment-786481 Share on other sites More sharing options...
seventheyejosh Posted March 17, 2009 Author Share Posted March 17, 2009 Ok, adding str_split instead of explode allows me to create the array, but implode isnt joining it. what is the counterpart of str_split? Quote Link to comment https://forums.phpfreaks.com/topic/149770-solved-first-recursive/#findComment-786483 Share on other sites More sharing options...
seventheyejosh Posted March 17, 2009 Author Share Posted March 17, 2009 Ok, implode worked fine i just had to add back in the & that you had. What exactly does it do? i cant seem to find any readily availible info on php.net and i've never used it? Thanks for the code as well Quote Link to comment https://forums.phpfreaks.com/topic/149770-solved-first-recursive/#findComment-786485 Share on other sites More sharing options...
Floydian Posted March 17, 2009 Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149770-solved-first-recursive/#findComment-786488 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.