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!
Sunday, January 13, 2008
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:
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
Subscribe to:
Posts (Atom)