Code Monkey home page Code Monkey logo

punycode's Introduction

Punycode

Software License GitHub Workflow Status Code Coverage PHP Version Total Downloads

An implementation of RFC 3492 Punycode in PHP, based on the sample implementation in Appendix C. This is NOT a substitue for idn_to_ascii and idn_to_utf8.

Requirements

  • PHP 7.1+

Installation

composer require rowbot/punycode

API

Punycode::decode(string $input, int $outLength = null, array &$caseFlags = [])

The Punycode::decode() method takes an ASCII encoded string and decodes it to a UTF-8 encoded string. Optionally, the second parameter can be specified to place a limit on the size of the returned string.

Parameters

  • $input - An ASCII encoded punycode string to convert to a UTF-8 encoded string.
  • $outLength - A positive integer representing the maximum length, in code points, of the resulting output string. Defaults to 2,147,483,647.
  • $caseFlags - An array, which will have the case flag of each character inserted into it.

Throws

  • \Rowbot\Punycode\Exception\OutputSizeExceededException - If the size of the output string exceeds the maximum size specified.
  • \Rowbot\Punycode\Exception\OverflowException - If integer overflow occurs.
  • \Rowbot\Punycode\Exception\InvalidInputException - If input contains non-ASCII bytes or mapping a code point to a digit fails.
use Rowbot\Punycode\Punycode;

try {
    echo Punycode::decode('Hello-Another-Way--fc4qua05auwb3674vfr0b'); // Hello-Another-Way-それぞれの場所
} catch (\Rowbot\Punycode\Exception\PunycodeException $e) {
    echo 'An error occured!';
}

Punycode::encode(string $input, int $outLength = null, array $caseFlags = [])

The Punycode::encode() method takes a UTF-8 encoded string and converts it into an ASCII encoded punycode string. Optionally, the second parameter can be specified to place a limit on the size of the returned string.

Parameters

  • $input - A UTF-8 encoded string to convert to punycode.
  • $outLength - A positive integer representing the maximum length, in code points, of the resulting output string. Defaults to 2,147,483,647.
  • $caseFlags - An array of bools where true indicates that the character should be uppercase and false indicates that it should be lowercase. This only affects ASCII characters [a-zA-Z].

Throws

  • \Rowbot\Punycode\Exception\OutputSizeExceededException - If the size of the output string exceeds the maximum size specified.
  • \Rowbot\Punycode\Exception\OverflowException - If integer overflow occurs.
use Rowbot\Punycode\Punycode;

try {
    echo Punycode::encode('Hello-Another-Way-それぞれの場所'); // Hello-Another-Way--fc4qua05auwb3674vfr0b
} catch (\Rowbot\Punycode\Exception\PunycodeException $e) {
    echo 'An error occured!';
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.