I'm use this function time to time and use in the db class.
If somebody may propose change somthing lets disscuse about it.
Code: Select all
<?php
//--[ Function: mod_array2string ]-------------------------------------------------------
#
# @purpose: Convert array to string or return false or error message if debug mode is active.
#
# @usege: mod_array2string($arrays [,$delimiter[,$pair]])
# @access: private
# @usedef: boolean MOD_AUTOPAIR if on and even pair put auto comma ", "
# @usedef: boolean MOD_DEBUG debug mode output error message if they exists
# @param: string $arrays table arrays
# @param: string $delimiter delimiter betwen elements default is comma ", "
# @param: string $pair set special delimiter between values as 1=2,3=4,etc.
# @param: string $keys try save array structure as key = value
# @param: return $return_values return parsed result or return invoked string
#
/*
Example 1:
DEFINE ('MOD_AUTOPAIR','on');
$dd = array("q","w","e","r");
echo mod_array2string($dd," = ");
out: q = w, e = r
-------------------------------------------------------
Example 2:
DEFINE ('MOD_AUTOPAIR','off');
$dd = array("q","w","e","r");
echo mod_array2string($dd," = ");
out: q = w = e = r
-------------------------------------------------------
Example 3:
$dd = array("q"=>8,"w"=>"fgh","e"=>71,"r"=>'SsA');
echo mod_array2string($dd," = ",", ",keys);
out: q = 8, w = fgh ,e = 71, r = SsA
-------------------------------------------------------
Example 4:
$dd = array("q"=>8,"w"=>"fgh","e"=>71,"r"=>'SsA');
echo mod_array2string($dd," = ",", ",true,"`");
out: `q` = `8`, `w` = `fgh`, `e` = `71`, `r` = `SsA`
=======================================================
*/
function mod_array2string($arrays, $delimiter = ', ', $pair = false, $keys = false, $quote = false)
{
if (is_array($arrays))
{ // begin if
$d = $delimiter;
$return_value = null;
$coutntq = count($arrays);
$qc = 0;
$def = ', ';
reset($arrays);
while (list($k,$v) = each($arrays))
{ // begin while
if (!is_integer($k) and $keys == true)
{ // begin if
if ($pair !=null)
{ // begin if
$delimiter = $pair;
} // end if
elseif (MOD_AUTOPAIR == "on")
{ // begin elseif
$delimiter = $def;
} //end elseif
if ($quote)
{ // begin if
$k = mod_2quotes($k, $quote);
$v = mod_2quotes($v, $quote);
} // end if
$return_value .= $k . $d . $v . $delimiter;
$delimiter = $d;
} //end if
else
{ // begin else
if ( (is_float($qc / 2)) and (!is_float($coutntq / 2)) )
{ // begin if
if ($pair !=null)
{ // begin if
$delimiter = $pair;
} // end if
elseif (MOD_AUTOPAIR == "on")
{ // begin elseif
$delimiter = $def;
} // end elseif
} // end if
$qc += 1;
if ($quote)
{ // begin if
$v = mod_2quotes($v, $quote);
} // end if
$return_value .= $v . $delimiter;
$delimiter = $d;
} // end else
} // end while
$return_value = rtrim($return_value,$def); // remove last default delimiter
$return_value = rtrim($return_value,$delimiter); // remove last defined delimiter
$return_value = rtrim($return_value,$pair); // remove last pair delimiter
} // end if
elseif(MOD_DEBUG == true)
{ // begin elseif
$return_value = 'Value of $arrays variable (<strong>'. $arrays .'</strong>) in ' .
'function mod_array2string($arrays [,<I>string</I> $delimiter [,<I>string</I> '.
'$pair]) is empty or not is array';
} // end elseif
else
{ // begin else
$return_value = false;
} // end else
return $return_value;
}
//--[ End of mod_array2string function ]-------------------------------------------------
//--[ Function: mod_string2array ]-------------------------------------------------------
#
# @purpose: Convert string to array or return false or error message if debug mode is active
#
# @usege: mod_string2array($string [,$delimiter])
# @access: private
# @usedef: boolean MOD_DEBUG Debug mode output error message if they exists
# @param: string $string string with delimeter
# @param: string $delimiter delimiter betwen elements default is comma ","
# @param: return $return_values return parsed result or return invoked string
#
# NOTE: be sure what delimiter is correspond with real delimiter in $string variable. Draw
# attention at the space after or before delimiter in the $string and $delimiter variables
/*
Example 1:
$str = "ff, gg, rr, hh";
print_r(mod_string2array($str,", "));
out:
Array
(
[0] => ff
[1] => gg
[2] => rr
[3] => hh
)
Example 2:
$str = "ff,gg,rr,hh";
print_r(mod_string2array($str,", "));
out:
Array
(
[0] => ff,gg,rr,hh
)
*/
function mod_string2array ($string, $delimiter = ", ")
{
if ( strlen(trim($string)) > 0 )
{ // begin if
$return_value = explode($delimiter, $string);
} // end if
elseif(MOD_DEBUG == true)
{ // begin elseif
$return_value = 'Value of <b>$string</b> variable in function <b>mod_string2array($string'.
' [,$delimiter])</b> is empty!';
} //end elseif
else
{ // begin else
$return_value = false;
} // end else
return $return_value;
}
//--[ End of mod_string2array function ]-------------------------------------------------
//--[ Function: mod_string2array ]-------------------------------------------------------
#
# @purpose: Convert string to array or return false or error message if debug mode is active
#
# @usege: mod_string2array($string [,$type])
# @access: private
# @usedef: boolean MOD_DEBUG Debug mode output error message if they exists
# @param: string $string string
# @param: string $delimiter quote or any char
# @param: return $return_values return parsed result or return invoked string
#
/*
Example 1:
echo "acronym of PHP - " . mod_2quotes("PHP: Hypertext Preprocessor") . ".";
out:
acronym of PHP - "PHP: Hypertext Preprocessor".
Example 2:
echo "SELECT " . mod_2quotes('field1','`') . " FROM " . mod_2quotes('table1','`');
out:
SELECT `field1` FROM `table1`
*/
function mod_2quotes($string, $type = '"')
{
if ( strlen(trim($string)) > 0 )
{ // begin if
$return_value = $type . $string . $type;
} // end if
else
{ // begin else
$return_value = false;
} // end else
return $return_value;
}
//--[ End of mod_2quotes function ]------------------------------------------------------
?>