Showing posts with label List Functions. Show all posts
Showing posts with label List Functions. Show all posts

Sunday, January 13, 2008

ListFind / ListFindNoCase

The ListFind and ListFindNoCase are identical functions other than the obvious reason; one is case sensative and the other is not. If you use list to store data or move data, the ListFind function is incredibly useful. Also, the ListFind function works well when rerading comma seperated lists or scraping data from other websites.

The Function:


<?php

function ListFind($list,$value,$delimiter=",")
{
$delimiter = substr($delimiter,1);
$a = explode($delimiter,$list);
for($i=0;$i<count($a);$i++)
{
if( strstr($a[$i],$value))
{
return true ;
}
}
return false;
}

function ListFindNoCase($list,$value,$delimiter=",")
{
$a = explode($delimiter,$list);
for ($i=0;$i<count($a);$i++)
{
if(stristr($a[$i],$value))
{
return true;
}
}
return false;
}


?>

Usage:

<?php
$list = "Car,Truck,Boat,Plane,Train" ;
$find = "car";
if(ListFind($list,$find))
{
echo $find . " found!";
}
?>


Using the ListFind to find 'car' will fail since the ListFind is case sensetive. However, the ListFindNoCase will do the trick!

<?php
$list = "Car,Truck,Boat,Plane,Train" ;
$find = "car";
if( ListFindNoCase($list,$find))
{
echo $find . " found!";
}
?>

The result in this case will be : car found!

Saturday, January 5, 2008

ListCount

The ListCount feature is a very basic function. The ListCount function merely reduces 2 lines of code into one and standardizes the way you count list elements. The ListCount function comes in hand when reading through a CSV file or saving data in a list. This function should operate in PHP in the same manner as ColdFusion!

The Function:

<?php

function ListCount($list, $delimiter="")
{
if($delimiter == "")
{
$delimiter = ",";
}
$a = explode($delimiter,$list);
return count($a);
}


?>

Usage:
Using the ListCount function is simple.

<?php

$list = "a,b,c,d";
echo ListCount($list);

?>

The Result: 4

Or, you can declare a delimiter:

<?php
$list = "CATDOGMOUSEFOXHORSE";
echo ListCount($list);
?>
The Result: 5

Monday, December 24, 2007

ListDeleteAt

The ListDeleteAt function is a neat little coldfusion function and works just as well in PHP. This function is good for removing counted values from lists. I use it a lot in conjunction with other list functions.
The Function:

<php
function ListDeleteAt($list,$position,$delimiter)
{
$delimiter = substr($delimiter, 1);
$a = explode($delimiter,$list);
for($i=0;$i<count($a);$i++)

{
if($i != $position)
{
if(! isset($return))
{
$return = $a[$i];
}
else
{
$return .= $delimiter . $a[$i];
}
}
}
return $return;
}
?>

Usage:

To use the ListDeleteAt function with the default delimiter ',':

<php
$list = "a,b,c,d,e,f,g";

$position = 3;
echo ListDeleteAt($list,$position);
?>
The Result: a,b,d,e,f,g. The 'c' which was in the third position has noe been removed!

To use the ListDeleteAt function with a custom delimiter:

<php
$list = "12345";

$position = 2;
echo ListDeleteAt($list,$position);
?>

The Result: '1345'. The '2' which was in the second position has noe been removed!

Saturday, December 22, 2007

ListContains

The ListContains function is a quick and easy function to use. The ListContains function determines the index of the first list element that contains the specified substring. If the substring can not be found in the list, The ListContains function will returns zero. This function will not ignore empty sets; thus the list "a,b,c,,,d" has 8 elements
 
The Function:
<php
function ListContains($list,$substring,$delimiter=",")
{
    $delimiter = substr($delimiter,1);
    $a = explode($list,$delimiter);
    $return = 0;
    for($i=0;$i< count($a);$i++)
    {
        if(strstr($a[$i],$substring))
        {
            $return = $i + 1;
        }
    }
    return $return;
}

?>
Usage:
Using the ListContains function is simple:
 
<?php
    $list = "one,two,three";
    $substr = "two" ; 
    if(ListContains($list,$substr) > 0)
    {
        echo $substr . " found at position " . ListContains($list,$substr):
    }
?>

The output of this function will be: two found at position 2

ListAppend

ListAppend is a rather simple utility function, however when you are dealing with lists, it can prove a great ally. This is a function that I pulled out of a class of list functions. I will publish the entire class once I get the individual functions published. The default delimiter for the ListAppend function is a ','. The delimiter length should be 1 character. Regardless of the size of the delimiter, the function will only use the first character.

The Function:

<php

function ListAppend($list,$value,$delimiter= ",")
{
$delimiter = substr($delimiter,1);
$nList = $list . $delimiter . $value;
return $nList;
}

?>

Usage:
StatementOutputComment
ListAppend('elem1,elem2', '' )elem1,elem2,Appended element is empty; delimiter is last character in list; list length is 2.
ListAppend('', 'elem1,elem2' )elem1,elem2List length is 2.
ListAppend("one___two", "three", "___")"one___two_three"Inserted the first character of delimiters before "three."

ListAppend works great to add values to a list. This function will default to a ',' for a delimiter!

For Example:

<?php
$list = "abcd";
$value = "Z";
echo ListAppend($list,$value);
?>


The results is 'abcd,Z'!

By sending the correct delimiter through, in this case '':

<?php
$list = "abcd";
$value = "Z" ;
echo ListAppend($list,$value,"");
?>


The results is 'abcdZ'!

Sunday, December 16, 2007

ListGetAt

ListGetAt is a really cool ColdFusion function. Is great for grabbing values from a comma separated list (or any other delimited list)! I use this function a lot instead of the explode function. They essentially do the same, but using this function removes 1 step. Instead of first exploding the then getting the value for your array key. For the seasoned PHP coder and everyone else, the position will always start at 1 NOT 0. Also, the default delimiter is a ','(comma).

The Function:

function ListGetAt($list,$position,$delimiter=",")
{
$bit = explode($delimiter,$list);
return $bit[($position-1)];
}

Usage:

Using the ListGetAt function is quite easy!

<?php
$list =
"apples,oranges,grapes";
echo ListGetAt($list,1);
?>


The resulting out put would be: apples

<?php
$list = "apples+oranges+grapes";
echo ListGetAt($list,2,"+");
?>

The resulting out put would be: oranges