Page 1 of 1

Functions in php and how to get them to run?

Posted: Wed May 14, 2008 2:44 pm
by abbeycatblue
I have the following code that I would like to adapt but I cannot seem to get it to run from a simple php page? Can anyone in simple language tell me how this is done. I can use PHP in a simple format but this has me stumped me as to how it is triggered?
Any help much appreciated.
:roll:
function strpos_array($haystack, $needle)
{
$kill = 0; // Kills while loop when changed
$offset = 0; // Offset for strpos()
$i = 0; // Counter, not iterator

while ($kill === 0)
{
$i++;
$result = strpos($haystack, $needle, $offset);

if ($result === FALSE)
{ // If result is false (no more instances found), kill the while loop
$kill = 1;
} else {
$array[$i] = $result; // Set array
$offset = $result + 1; // Offset is set 1 character after previous occurence
}

}

return $array;

}

Re: Functions in php and how to get them to run?

Posted: Wed May 14, 2008 3:22 pm
by JacobT
hey abbey, you don't actually need the kill variable or the iterator. Here's an adaptation of your code. It could be refined a little more, but that's your job ;)

Code: Select all

function strpos_array($haystack, $needle) {
    $offset = 0; // Offset for strpos()
 
    while($i = 1) {
        $result = strpos($haystack, $needle, $offset);
 
            // If noting is found break the while loop
        if ($result === false) {
            break;
        } else {
            $array[] = $result; // Set array
            // Offset is set 1 character after previous occurence
            $offset = $result + 1;
        }
    }
    return $array;
}
 
var_dump(strpos_array("this is a test", "s"));
basically the while loop always runs, because $i is always able to be set to 1 (notice it's a single = not a double). When the result is false, just use 'break;' and that breaks out of the current loop. You could also just return $array; there also, that will end the loop too. You don't need the iterator because using the $array[] means just add another record in the next slot.

Let me know if you have any questions.

Re: Functions in php and how to get them to run?

Posted: Wed May 14, 2008 3:33 pm
by abbeycatblue
Thanks Jacob for your prompt reply and suggetsed improved code. The last line of your coding is the key for me and as I can now see what is happening, brilliant, many thanks. :lol:

Re: Functions in php and how to get them to run?

Posted: Wed May 14, 2008 3:43 pm
by JacobT
hey abbey, you can see the function being called at the very bottom of my code

Code: Select all

var_dump(strpos_array("this is a test", "s"));
that could be written as this as well:

Code: Select all

$resultArray = strpos_array("this is a test", "s");
Hope that helps!

Re: Functions in php and how to get them to run?

Posted: Wed May 14, 2008 11:20 pm
by dbemowsk
Abbey,

Here is your code simplified just a little more than Jacob's code

Code: Select all

 
<?PHP
function strpos_array($haystack, $needle)
{
    $result = TRUE; //Used to make sure that the loop will not die on the first run
    $offset = 0; // Offset for strpos()
    $array = array(); //It is a good idea to initialize $array to an empty array in the event that there is no result
    
    while ($result !== false)
    {
        $result = strpos($haystack, $needle, $offset);
    
        if ($result !== FALSE)
        { //If the $result is false then the if statement will fail, as well, the while loop will exit
            $array[] = $result; // Add the result to the array stack
            $offset = $result + 1; // Offset is set 1 character after previous occurrence
        }
    }
    
    return $array;t
 
}
 
//This one will return nothing.  If $array was not initialized this would return an undefined variable notice
print_r(strpos_array("this is a test", "p"));  
 
/*
The line below will return 
Array
(
    [0] => 0
    [1] => 10
    [2] => 13
)
*/
print_r(strpos_array("this is a test", "t"));
 
?>
 

Re: Functions in php and how to get them to run?

Posted: Thu May 15, 2008 12:18 am
by abbeycatblue
dbemowsk,
Thanks for your input, very useful.
A