Sunday, January 13, 2008
ListFind / ListFindNoCase
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 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:
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 { |
| 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 | ||||||||||||
| Usage: | ||||||||||||
| ||||||||||||
ListAppend works great to add values to a list. This function will default to a ',' for a delimiter! <?php |
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