utf8_uri_encode

Definition:
function utf8_uri_encode( $utf8_string, $length = 0 ) {}

Encode the Unicode values to be used in the URI.

Parameters

  • string $utf8_string
  • int $length: Max length of the string

Return values

returns:String with Unicode encoded for URI.

Source code

function utf8_uri_encode( $utf8_string, $length = 0 ) {

	$unicode = '';

	$values = array();

	$num_octets = 1;

	$unicode_length = 0;



	$string_length = strlen( $utf8_string );

	for ($i = 0; $i < $string_length; $i++ ) {



		$value = ord( $utf8_string[ $i ] );



		if ( $value < 128 ) {

			if ( $length && ( $unicode_length >= $length ) )

				break;

			$unicode .= chr($value);

			$unicode_length++;

		} else {

			if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;



			$values[] = $value;



			if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )

				break;

			if ( count( $values ) == $num_octets ) {

				if ($num_octets == 3) {

					$unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);

					$unicode_length += 9;

				} else {

					$unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);

					$unicode_length += 6;

				}



				$values = array();

				$num_octets = 1;

			}

		}

	}



	return $unicode;

}

3309

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: