Encodes an entry for storage in our database (vcard 3.0 format, unfolded)

@param array Raw data structure to encode

@return string vCard encoded string
14247
static function vcard_encode($data)
    {
        foreach ((array)$data as $type => $entries) {
            // valid N has 5 properties
            while ($type == "N" && is_array($entries[0]) && count($entries[0]) < 5) {
                $entries[0][] = "";
            }

            // make sure FN is not empty (required by RFC2426)
            if ($type == "FN" && empty($entries)) {
                $entries[0] = $data['EMAIL'][0][0];
            }

            foreach ((array)$entries as $entry) {
                $attr = '';
                if (is_array($entry)) {
                    $value = array();
                    foreach ($entry as $attrname => $attrvalues) {
                        if (is_int($attrname)) {
                            if (!empty($entry['base64']) || $entry['encoding'] == 'B') {
                                $attrvalues = base64_encode($attrvalues);
                            }
                            $value[] = $attrvalues;
                        }
                        else if (is_bool($attrvalues)) {
                            // true means just a tag, not tag=value, as in PHOTO;BASE64:...
                            if ($attrvalues) {
                                // vCard v3 uses ENCODING=b (#1489183)
                                if ($attrname == 'base64') {
                                    $attr .= ";ENCODING=b";
                                }
                                else {
                                    $attr .= strtoupper(";$attrname");
                                }
                            }
                        }
                        else {
                            foreach ((array)$attrvalues as $attrvalue) {
                                $attr .= strtoupper(";$attrname=") . self::vcard_quote($attrvalue, ',');
                            }
                        }
                    }
                }
                else {
                    $value = $entry;
                }

                // skip empty entries
                if (self::is_empty($value)) {
                    continue;
                }

                $vcard .= self::vcard_quote($type) . $attr . ':' . self::vcard_quote($value) . self::$eol;
            }
        }

        return 'BEGIN:VCARD' . self::$eol . 'VERSION:3.0' . self::$eol . $vcard . 'END:VCARD';
    }
220785
public static function encodePhp( array $data ) {
		$ret = '';
		foreach ( $data as $key => $value ) {
			if ( strcmp( $key, intval( $key ) ) === 0 ) {
				self::$logger->warning( "Ignoring unsupported integer key \"$key\"" );
				continue;
			}
			if ( strcspn( $key, '|!' ) !== strlen( $key ) ) {
				self::$logger->error( "Serialization failed: Key with unsupported characters \"$key\"" );
				return null;
			}
			$v = self::serializeValue( $value );
			if ( $v === null ) {
				return null;
			}
			$ret .= "$key|$v";
		}
		return $ret;
	}
1083273
function ucs2encode(array) {
  return _.map(array, function (value) {
    var output = '';

    if (value > 0xFFFF) {
      value -= 0x10000;
      output += String.fromCharCode(value >>> 10 & 0x3FF | 0xD800);
      value = 0xDC00 | value & 0x3FF;
    }
    output += String.fromCharCode(value);
    return output;
  }).join('');
}
419590
public static function csv(array $array, string $separator = "\t", string $encapsulate_cells = '"') :string {

		// declare our csv
		$csv = '';
		// for each line of the array
		foreach($array as $cells) {
			// for each cell of that line
			foreach($cells as $cell) {
				// protect the cell's value, encapsulate it, and separate it,
				$csv .= $encapsulate_cells . str_replace($encapsulate_cells, '\\'.$encapsulate_cells, $cell) . $encapsulate_cells . $separator;
			}
			// skip to the next line
			$csv .= "\n";
		}
		// return the formatted csv
		return $csv;

	}
364575
protected static function encodeArray($array)
    {
        if (Arrays::isAssoc($array)) {
            $output = [];
            foreach ($array as $key => $val) {
                $output[] = $key . ':' . \addcslashes($val, ': ');
            }

            return self::encodeString(\implode(' ', $output));
        }

        $output = [];
        foreach ($array as $val) {
            $output[] = \addcslashes($val, ': ');
        }

        return self::encodeString(\implode(' ', $output));
    }
301649
public function encode(array $row): string
    {
        $stream = fopen('php://memory', 'r+');
        fputcsv($stream, $row, "\t");
        rewind($stream);
        $line = fgets($stream);
        fclose($stream);
        return $line;
    }
902664
function encode($decoded, $one_time_encoding = false)
    {
        // Forcing conversion of input to UCS4 array
        // If one time encoding is given, use this, else the objects property
        switch ($one_time_encoding ? $one_time_encoding : $this->_api_encoding) {
        case 'utf8':
            $decoded = $this->_utf8_to_ucs4($decoded);
            break;
        case 'ucs4_string':
           $decoded = $this->_ucs4_string_to_ucs4($decoded);
        case 'ucs4_array':
           break;
        default:
            $this->_error('Unsupported input format: '.($one_time_encoding ? $one_time_encoding : $this->_api_encoding));
            return false;
        }

        // No input, no output, what else did you expect?
        if (empty($decoded)) return '';

        // Anchors for iteration
        $last_begin = 0;
        // Output string
        $output = '';
        foreach ($decoded as $k => $v) {
            // Make sure to use just the plain dot
            switch($v) {
            case 0x3002:
            case 0xFF0E:
            case 0xFF61:
                $decoded[$k] = 0x2E;
                // Right, no break here, the above are converted to dots anyway
            // Stumbling across an anchoring character
            case 0x2E:
            case 0x2F:
            case 0x3A:
            case 0x3F:
            case 0x40:
                // Neither email addresses nor URLs allowed in strict mode
                if ($this->_strict_mode) {
                   $this->_error('Neither email addresses nor URLs are allowed in strict mode.');
                   return false;
                }

                // Skip first char
                if ($k) {
                    $encoded = '';
                    $encoded = $this->_encode(array_slice($decoded, $last_begin, (($k)-$last_begin)));
                    if ($encoded) {
                        $output .= $encoded;
                    } else {
                        $output .= $this->_ucs4_to_utf8(array_slice($decoded, $last_begin, (($k)-$last_begin)));
                    }
                    $output .= chr($decoded[$k]);
                }
                $last_begin = $k + 1;
            }
        }
        // Catch the rest of the string
        if ($last_begin) {
            $inp_len = sizeof($decoded);
            $encoded = '';
            $encoded = $this->_encode(array_slice($decoded, $last_begin, (($inp_len)-$last_begin)));
            if ($encoded) {
                $output .= $encoded;
            } else {
                $output .= $this->_ucs4_to_utf8(array_slice($decoded, $last_begin, (($inp_len)-$last_begin)));
            }
            return $output;
        }

        if ($output = $this->_encode($decoded)) {
            return $output;
        }

        return $this->_ucs4_to_utf8($decoded);
    }
778220
public function encodeSegment(array $row): string
    {
        // init
        $str = '';
        $t = \count($row);

        foreach ($row as $i => &$iValue) {
            if (\is_array($iValue)) {
                if (\count($iValue) === 1
                    && \is_array(\reset($iValue))
                ) {
                    $iValue = \array_pop($iValue);
                }

                if (\is_array($iValue)) {
                    foreach ($iValue as &$temp) {
                        $temp = $this->escapeValue($temp);
                    }
                    unset($temp);
                }

                $elm = \implode($this->sepComp, $iValue);
            } else {
                $elm = $this->escapeValue($iValue);
            }

            $str .= $elm;
            if ($i == $t - 1) {
                break;
            }
            $str .= $this->sepData;
        }
        unset($iValue);

        $str .= $this->symbEnd;

        return $str;
    }
849748
private static function encodeObjects($d)
    {
        if ($d instanceof Api) {
            return self::utf8(Common::getClassName(get_class($d)));
        } else if ($d === true) {
            return 'true';
        } else if ($d === false) {
            return 'false';
        } else if (is_array($d)) {
            $res = array();
            foreach ($d as $k => $v) {
                $res[$k] = self::encodeObjects($v);
            }
            return $res;
        } else {
            return self::utf8($d);
        }
    }
86180
public static function encode($string)
    {
        if (is_array($string)) {
            foreach ($string as $k => $substring) {
                $string[$k] = static::encode($substring);
            }
        } else {
            $string = urlencode($string);
        }

        return $string;
    }
863124
public function encode($d)
	{
		if (is_array($d)) {
			$def = 'd';
			$s = '';

			// If first array key is an integer, assume list.
			$list = false;
			if (is_int(array_keys($d)[0])) {
				$list = true;
				$def = 'l';
			}

			ksort($d, SORT_STRING);

			foreach ($d as $key => $value) {
				if (is_string($key)) {
					if ($list) {
						throw new Exception('Invalid bencode data.', 1);
					}
					$s .= strlen($key) . ':' . $key;
				}
				if (is_int($value) || is_float($value)) {
					$s .= 'i' . $value . 'e';
				} elseif (is_string($value)) {
					$s .= strlen($value) . ':' . $value;
				} else {
					$s .= $this->encode($value);
				}
			}

			return $def . $s . 'e';
		} elseif (is_int($d)) {
			return 'i' . $d . 'e';
		} elseif (is_string($d)) {
			return strlen($d) . ':' . $d;
		} else {
			throw new Exception('Invalid data provided to Bencode.', 1);
		}
	}
536525
public function writeArray(&$array)
    {
        // arrays aren't reference here but still counted
        $this->_referenceObjects[] = $array;

        // have to seperate mixed from numberic keys.
        $numeric = array();
        $string  = array();
        foreach ($array as $key => &$value) {
            if (is_int($key)) {
                $numeric[] = $value;
            } else {
                $string[$key] = $value;
            }
        }

        // write the preamble id of the array
        $length = count($numeric);
        $id     = ($length << 1) | 0x01;
        $this->writeInteger($id);

        //Write the mixed type array to the output stream
        foreach($string as $key => &$value) {
            $this->writeString($key)
                 ->writeTypeMarker($value);
        }
        $this->writeString($this->_strEmpty);

        // Write the numeric array to ouput stream
        foreach($numeric as &$value) {
            $this->writeTypeMarker($value);
        }
        return $this;
    }
132054
private static function superEncode($array)
    {
        if (is_array($array) && !empty($array)) {
            foreach ($array as $key => $value) {
                if (is_array($value)) {
                    $array[$key] = self::superEncode($value);
                } else {
                    $array[$key] = mb_convert_encoding($value, 'UTF-8', 'UTF-8');
                }
            }
        }

        return $array;
    }
624891
public function encode($data)
    {
        $result = '';
        foreach($data as $k => $v)
        {
            $result .= $k . '|' . serialize($v);
        }
        return $result;
    }
787093
public static function vcard_quote($s, $sep = ';')
    {
        if (is_array($s)) {
            foreach ($s as $part) {
                $r[] = self::vcard_quote($part, $sep);
            }
            return(implode($sep, (array)$r));
        }

        return strtr($s, array('\\' => '\\\\', "\r" => '', "\n" => '\n', $sep => '\\'.$sep));
    }
145370
function assoc_to_uri($array) {
		$temp=array ();
		foreach ( ( array ) $array as $key => $val ) {
			$temp[]=$key;
			$temp[]=$val;
		}

		return implode('/', $temp);
	}
253766
private function serializeArrayOrTable(array $array)
    {
        $assoc = false;
        $index = 0;
        $values = [];

        foreach ($array as $key => $value) {
            // We already know the array is associative, serialize both the key
            // and the value ...
            if ($assoc) {
                $values[] = $this->serializeShortString($key)
                    . $this->serializeField($value);

                // Otherwise, if the key matches the index counter it is sequential,
                // only serialize the value ...
            } elseif ($key === $index++) {
                $values[] = $this->serializeField($value);

                // Otherwise, we've just discovered the array is NOT sequential,
                // Go back through the existing values and add the keys ...
            } else {
                foreach ($values as $k => $v) {
                    $values[$k] = $this->serializeShortString($k) . $v;
                }

                $values[] = $this->serializeShortString($key)
                    . $this->serializeField($value);

                $assoc = true;
            }
        }

        return ($assoc ? 'F' : 'A') . $this->serializeByteArray(
                implode('', $values)
            );
    }