You are here

function bencode in Services 7.3

Same name and namespace in other branches
  1. 6.3 servers/rest_server/lib/bencode.php \bencode()

Bencode snippet from http://paste.lisp.org/display/17178

1 call to bencode()
ServicesBencodeFormatter::render in servers/rest_server/includes/ServicesFormatter.inc
Render data to the string.

File

servers/rest_server/lib/bencode.php, line 6

Code

function bencode($element) {
  $out = "";
  if (is_numeric($element) && $element == intval($element)) {
    $out = 'i' . $element . 'e';
  }
  else {
    if (is_string($element)) {
      $out = strlen($element) . ':' . $element;
    }
    else {
      if (is_array($element) || is_object($element) && ($element = get_object_vars($element))) {
        ksort($element);
        if (is_string(key($element))) {
          $out = 'd';
          foreach ($element as $key => $val) {
            $out .= bencode($key) . bencode($val);
          }
          $out .= 'e';
        }
        else {
          $out = 'l';
          foreach ($element as $val) {
            $out .= bencode($val);
          }
          $out .= 'e';
        }
      }
    }
  }
  return $out;
}