string2array and array2string

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

string2array and array2string

Postby WiZARD » Thu May 12, 2005 6:05 am

OPINION
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 ]------------------------------------------------------
?>
Last edited by WiZARD on Mon May 16, 2005 7:43 am, edited 1 time in total.

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Mon May 16, 2005 7:38 am

looks pretty good..however description of the last function is totaly from the previous function ^^

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Postby WiZARD » Mon May 16, 2005 7:43 am

Alexei Kubarev wrote:looks pretty good..however description of the last function is totaly from the previous function ^^

ouch!
Danke!

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Mon May 16, 2005 7:45 am

Bitte

User avatar
GedroX
New php-forum User
New php-forum User
Posts: 49
Joined: Fri Apr 07, 2006 1:15 am

Postby GedroX » Fri Apr 07, 2006 2:01 am

What's wrong with serialize(), unserialize() functions???

sanketce
New php-forum User
New php-forum User
Posts: 90
Joined: Fri Dec 02, 2005 3:23 am

Postby sanketce » Fri Apr 07, 2006 4:05 am

Nice code !!! Thanks for posting.

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Fri Apr 07, 2006 4:10 am

well i guess just for another tipe of serialization... still not sure why... but still..


Return to “Your Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest

cron