Example code for string and multi-input conversion functions in PHP

Source: Internet
Author: User
Tags sprintf
Conversion functions

/** * [string converted to (2,8,16) ASCII code] * @param string $str [Pending String] * @param boolean $encode [string converted to ascii| ASCII converted to string] * @param string $intType [2,8,16] * @return string byte_str [processing result] * @author Alexander */function Strtoas    CII ($str, $encode =true, $intType = "2") {if ($encode = = True) {$byte _array = Str_split ($STR);      foreach ($byte _array as & $value) {$value = ord ($value);          Switch ($intType) {case: $value = sprintf ("%02x", $value);        Break          Case 8: $value = sprintf ("%03o", $value);        Break          Default: $value = sprintf ("%08b", $value);      Break    }} unset ($value);  $byte _str = Implode (", $byte _array);    } else{$chunk _size = $intType = = = 2: ($intType = = 8? 3:8);    $byte _array = Chunk_split ($str, $chunk _size);    $byte _array = Array_filter (Explode ("\ r \ n", $byte _array)); foreach ($byte _array as & $value) {$fun _name = $intType = = 16? ' Hexdec ': ($intType = = 8? ' Octdec ': ' Bindec ');      $value = $fun _name ($value);    $value = Chr ($value);    } unset ($value);  $byte _str = Implode (", $byte _array); } return $byte _str;}

Multi-binary in PHP

PHP integer values can be expressed in decimal, hexadecimal, octal, or binary notation, preceded by an optional symbol (-or +).

Binary: [+-]?0b[01]+

Octal: [+-]?0[1-7]+

Decimal: [+-]? [1-9] [0-9]*|0

Hex: [+-]? [XX] [0-9a-fa-f]+

The multi-input conversion function:

The above is a small series for everyone to bring about the string in PHP and the multi-conversion function instance code all content, I hope that you support topic.alibabacloud.com~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.