src/Bencode.php
/**
     * Encode arbitrary data to bencode string
     *
     * @param mixed $data
     * @return string
     */
Encode arbitrary data to bencode string
@param mixed $data
@return string
Encode arbitrary data to bencode string @param mixed $data @return string
public static function encode($data)
    {
        if (is_array($data)) {
            $return = '';
            $check = -1;
            $list = true;
            foreach ($data as $key => $value) {
                if ($key !== ++$check) {
                    $list = false;
                    break;
                }
            }
            if ($list) {
                $return .= 'l';
                foreach ($data as $value) {
                    $return .= self::encode($value);
                }
            } else {
                $return .= 'd';
                ksort($data, SORT_STRING);
                foreach ($data as $key => $value) {
                    $return .= self::encode(strval($key));
                    $return .= self::encode($value);
                }
            }
            $return .= 'e';
        } elseif (is_integer($data)) {
            $return = 'i' . $data . 'e';
        } else {
            $return = strlen($data) . ':' . $data;
        }
        return $return;
    }
public static function encode ( $ data ) { if ( is_array ( $ data ) ) { $ return = '' ; $ check = - 1 ; $ list = true ; foreach ( $ data as $ key => $ value ) { if ( $ key !== ++ $ check ) { $ list = false ; break ; } } if ( $ list ) { $ return .= 'l' ; foreach ( $ data as $ value ) { $ return .= self :: encode ( $ value ) ; } } else { $ return .= 'd' ; ksort ( $ data , SORT_STRING ) ; foreach ( $ data as $ key => $ value ) { $ return .= self :: encode ( strval ( $ key ) ) ; $ return .= self :: encode ( $ value ) ; } } $ return .= 'e' ; } elseif ( is_integer ( $ data ) ) { $ return = 'i' . $ data . 'e' ; } else { $ return = strlen ( $ data ) . ':' . $ data ; } return $ return ; }
Encode arbitrary data to bencode string
 @param mixed $data
 @return string
Encode arbitrary data to bencode string @param mixed $data @return string