can a function call itself in php? [SOLVED *why yes, it can*
Posted: Tue Jul 22, 2008 12:33 pm
Hello,
I've run into an interesting problem. I wrote this little function to basically take a comma separated string (ex: value, value, another value, value) and make sure theres not an extra comma on the tail or the front (ex: value, value,)
Here is the function:
Note the function calls itself. Any page that calls this function does not even load. I get Unable to connect error from firefox, like the page isn't even there (which it is). I take the function out of the page, boom: it loads, no problem.
I'm running an apache server as installed by Xampp 1.6.6a
Also, if you can think of a nicer way to achieve what I'm after with the function, that'd be appreciated too! =]
I've run into an interesting problem. I wrote this little function to basically take a comma separated string (ex: value, value, another value, value) and make sure theres not an extra comma on the tail or the front (ex: value, value,)
Here is the function:
Code: Select all
function csvTrim($string){
$csv = trim($string);
$numOfCommas = substr_count($csv, ",");
$csvArray = explode(",", $csv);
foreach($csvArray as $value) {
if($value != ""){
$numOfValues++;
}
}
if($numOfCommas-1 >= $numOfValues) {
//more commas than values?! time to trim
if($csv[0] == ",") {
$trimedCsv = substr_replace($csv, '', 0,1); //deletes first character
$returnCsv = csvTrim($trumedCsv);
return $returnCsv;
}else{
$trimedCsv = substr_replace($csv, '', -1); //deletes last character
$returnCsv = csvTrim($trumedCsv);
return $returnCsv;
}
}else{
return $csv;
}
}I'm running an apache server as installed by Xampp 1.6.6a
Also, if you can think of a nicer way to achieve what I'm after with the function, that'd be appreciated too! =]