
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Convert ASCII TO UTF-8 Encoding in PHP?
If we know that the current encoding is ASCII, the 'iconv' function can be used to convert ASCII to UTF-8. The original string can be passed as a parameter to the iconv function to encode it to UTF-8.
Example
<?php $str = "ábrêcWtë"; echo 'Original :', ("$str"), PHP_EOL; echo 'Plain :', iconv("UTF-8", "ISO-8859-1", $str), PHP_EOL; ?>
A string with special characters is assigned to ‘str’ variable. This is passed to the ‘iconv’ function, with the encoding that it currently is in, and the encoding to which it needs to be converted to.
Output
This will produce the following output −
Original :ábrêcWtë Plain :�br�cWt�
Another method is to detect the encoding and then converting it to an appropriate encoding −
Example
$string = "ábrêcWtë"; print(mb_detect_encoding ($string)); $string = mb_convert_encoding($string, "UTF-8"); print(mb_detect_encoding ($string));
A string value with special characters is assigned to ‘string; variable. This is passed to the ‘mb_convert_encoding’ function that converts it to the target encoding.
Output
This will produce the following output −
UTF-8UTF-8
- Related Articles
- Convert Unicode to UTF-8 in Java
- Convert UTF-8 to Unicode in Java
- How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters in java?
- Convert String to UTF-8 bytes in Java
- How to convert wrongly encoded data to UTF-8 in MySQL?
- How to convert an MySQL database characterset and collation to UTF-8?
- UTF-8 Validation in C++
- PHP – Convert a string to a requested character encoding using iconv()
- 8085 program to convert 8 bit BCD number into ASCII Code
- Make PHP pathinfo() return the correct filename if the filename is UTF-8
- Detect base64 encoding in PHP?
- Change MySQL default character set to UTF-8 in my.cnf?
- How to set encoding in PHP FPDI library?
- How to read and write unicode (UTF-8) files in Python?
- 8085 Program to convert ASCII to HEX

Advertisements