PHP Salt


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

Version: 2.14.1
Last Build: March 10, 2017 13:28pm (PST)

psFile_kv()
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 = "=")
{
$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 = "=")
{
$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:

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