PHP Salt


A little dash of PHP... my collection of php functions

Version: 2.33.2
Last Build: December 29, 2023 20:28pm (PST)

Main Menu

Home

Get PHP Salt


The Functions

array
db
file
graph
html
mail
math
misc
mobile
script
string
system
time
web
psFileKeyVal()
Read Key=Value from txt file


Description:

Reads a file of keys and values, such as = or |
Default is '='
White spaces are ignored, and lines begining with # are also ignored

psFile_kv returns keys intact
psFile_kvl returns keys lowercase




Usage:


$aryTable = psFile_kv("/tmp/somefile.txt");
$aryTable = psFile_kvl("/tmp/somefile.txt");




Example:


Reads files in this format:

# user info
phone = 555-1212
name = mike
desc = that guy






Code:

// wrappers for old name (some people still using)
function psFile_kv($wFile,$d = "=") { return psFileKeyVal($wFile,$d); }
function psFile_kvl($wFile,$d = "=") { return psFileKeyValL($wFile,$d); }


function psFileKeyVal($wFile,$d = "=")
{
$res = array();
$ary = @file($wFile);
if ( is_array($ary) == true )
{
foreach ($ary as $line)
{
$line = trim($line);
if ( ($line !="") && (substr($line,0,1) != "#") )
{
list($key,$val) = explode($d,$line,2);
$key = trim($key); $val = trim($val);
$res[$key] = $val;
}
}
}
return $res;
}

function psFileKeyValL($wFile,$d = "=")
{
$res = array();
$ary = @file($wFile);
if ( is_array($ary) == true )
{
foreach ($ary as $line)
{
$line = trim($line);
if ( ($line !="") && (substr($line,0,1) != "#") )
{
list($key,$val) = explode($d,$line,2);
$key = strtolower(trim($key)); $val = trim($val);
$res[$key] = $val;
}
}
}

return $res;
}


Changlelog:

07/13/2012 added explode(,,2) to allow more than one delimiter to apear in result


See Also

  psFileKeyValWrite()
And a shot out to:

PHP - php.net
Fedora Server - getfedora.com
Shameless ads to pay for site