亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Backend Development PHP Tutorial PHP Master | Working with Multibyte Strings

PHP Master | Working with Multibyte Strings

Feb 23, 2025 am 09:08 AM

PHP Master | Working with Multibyte Strings

Number language, whether in English, Japanese or any other language, consists of many characters. Therefore, when dealing with a numeric language, a basic question is how to represent each character numerically. In the past, we only had to represent English characters, but now things are very different, and the result is a dazzling character encoding scheme to represent characters in multiple different languages. How does PHP associate and process these different schemes?

Key points

  • Multi-byte characters use one to four bytes to define characters, which is crucial for numeric representations of languages ??with more than 256 unique characters. Unicode, especially UTF-8, is the most commonly used encoding scheme for these characters.
  • PHP itself is not designed to handle multibyte characters. To process these characters, a special set of functions, the mbstring function, should be used. However, PHP's HTTP header also contains character set identifiers that can override the page's meta tags.
  • Multi-byte support is not the default feature of PHP and requires reconfiguration. To enable the mb function, use the --enable-mbstring compile-time option and set the runtime configuration option mbstring-encoding_translation.
  • Several multibyte string commands are available in PHP, such as mb_check_encoding, mb_strlen, and mb_ereg_search, which are used to check whether a specific encoding sequence is valid, find the number of characters in a multibyte string, and perform traditional character searches. Multibyte version.

Basics

We all know that "bits" can be 0 or 1, while "bytes" are a combination of eight consecutive bits. Since there are eight such double-valued bits in a byte, a byte can be configured in a total of 256 different modes (to the 8th power of 2). Different characters can be associated with each possible 8-bit mode. Put these bytes together in different orders and you have your own way of communicating. It's not necessarily smart, it depends on who is on both ends, but it's communication. As long as we can express characters in a language with 256 unique characters or less, we succeed. But what if we can't express a language with just 256 characters? Or what if we need to express multiple languages ??in the same document? Today, as we digitize everything we can find, 256 characters are far from enough. Fortunately, character schemes that better meet this challenge have been designed. These new supercharacter sets use one to four bytes to define characters. Today, the big guy in the field of character encoding is Unicode, which is a solution that uses multiple bytes to represent characters. It was developed by Unicode Consortium and comes in several versions: UTF-32 (for Dreadnaught Class Starship), UTF-16 (for Enterprise in Star Trek: Dark Unbound) and UTF-8 (most of us People should use it in the real world for our web applications). As I said, Unicode (including UTF-8) uses multiple byte configurations to represent characters. UTF-8 uses one to four bytes to generate 1,112,064 patterns to represent different characters. These "wide characters" take up more space, but UTF-8 tends to process faster than some other encoding schemes. Why do everyone praise UTF-8? Part of this is the popular models highlighted in UTF-8-enabled ads seen on ESPN and TCM, but mainly because UTF-8 mimics ASCII, which tracks ASCII precisely if you don’t involve any special characters.

How does this affect PHP?

I know what you are thinking. I just need to set the character set to "UTF-8" in my meta tag and everything will be fine. But this is not true. First, the simple fact is that PHP is not really designed to handle multibyte characters, so using standard string functions to operate on these characters can produce uncertain results. When we need to process these multibyte characters, we need to use a special set of functions: the mbstring function. Second, even if you control PHP, there may still be problems. The HTTP header that overrides your communication also contains a character set identity, which overrides the content in the page meta tag. So, how does PHP handle multibyte characters? There are two sets of functions that affect multibyte strings. The first one is iconv. Starting with version 5.0, this has become the default part of the language, a way to convert one character set to another character set representation. This is not what we will discuss in this article. The second is multibyte support, which is a series of commands prefixed with "mb_". There are many of these commands, and a quick review shows that some of them are related to determining whether characters are appropriate based on a given encoding scheme, while others are search-oriented functions similar to part of PHP regular expressions but are multibyte functions .

Enable multibyte support for PHP

Multi-byte support is not the default feature of PHP, but it also doesn't require us to download any additional libraries or extensions; it just requires some reconfiguration. Unfortunately, if you are using a managed version of PHP, this may not be something you can do. Use the phpinfo() function to view your configuration. Scroll down to output about halfway, and there will be a section called "mbstring". This will show you whether the basic features are enabled. For information on how to enable this feature, you can refer to the manual. In short, you can enable the mb function by using the --enable-mbstring compile-time option and set the runtime configuration option mbstring-encoding_translation. Of course, the final solution is PHP 6, as it will use IBM (please take off your hat) ICU library to ensure native support for multibyte character sets. All we have to do is sit down and wait, right? But until then, check out the multibyte support available now.

Multi-byte string command

There may be 53 different multibyte string commands. There may be 54. I was a little out of the way at some point, but you get what I mean. Needless to say, we won't explain it one by one, but for fun, let's take a quick look at a few.

  • mb_check_encoding
The

mb_check_encoding() function checks to determine whether a specific encoding sequence is valid for the encoding scheme. The function won't tell you how the string is encoded (or which schemes it will work for), but it will tell you whether it works for the specified scheme.

<?php
$string = 'u4F60u597Du4E16u754C';
$string = json_decode('"' . $string . '"');
$valid = mb_check_encoding($string, 'UTF-8');
echo ($valid) ? 'valid' : 'invalid';
?>

You can find a list of supported encodings in the PHP manual.

  • mb_strlen
The

strlen() function returns the number of bytes in the string. For ASCII, which is a single byte, this makes it nice to find the number of characters. For multibyte strings, you need to use the mb_strlen() function.

<?php
$string = 'u4F60u597Du4E16u754C';
$string = json_decode('"' . $string . '"');
$valid = mb_check_encoding($string, 'UTF-8');
echo ($valid) ? 'valid' : 'invalid';
?>
  • mb_ereg_search
The

mb_ereg_search() function performs a multibyte version of the traditional character search. But there are some caveats - you need to specify the encoding scheme using the mb_regex_encoding() function, the regular expression has no separator (it is just a pattern part), and both the regular expression and the string are specified using mb_ereg_search_init() .

<?php
$string = 'u4F60u597Du4E16u754C';
$string = json_decode('"' . $string . '"');

echo strlen($string); // 輸出 12 – 錯誤!
echo mb_strlen($string, 'UTF-8'); // 輸出 4
?>

Is it enough?

I don't know how you are, but I think the world really needs more simple things. Unfortunately, multibyte processing does not meet this requirement. But for now, this is something you can't ignore. Sometimes you won't be able to perform normal PHP string processing (because you're trying to process characters that exceed the normal ASCII range (U 0000 – U 00FF). This means you have to use mb_ oriented functions. Want to know more? Seriously, do you want to? I really thought this would scare you away. I was unprepared for this. My time has come. What is your best choice? Check out the PHP manual. Oh, and try something. There is nothing to replace the experience of actually using something.

(The original FAQ part should be retained here because its content is highly related to the topic of the article and will reduce readability after rewriting.)

The above is the detailed content of PHP Master | Working with Multibyte Strings. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

php regex for password strength php regex for password strength Jul 03, 2025 am 10:33 AM

To determine the strength of the password, it is necessary to combine regular and logical processing. The basic requirements include: 1. The length is no less than 8 digits; 2. At least containing lowercase letters, uppercase letters, and numbers; 3. Special character restrictions can be added; in terms of advanced aspects, continuous duplication of characters and incremental/decreasing sequences need to be avoided, which requires PHP function detection; at the same time, blacklists should be introduced to filter common weak passwords such as password and 123456; finally it is recommended to combine the zxcvbn library to improve the evaluation accuracy.

PHP Variable Scope Explained PHP Variable Scope Explained Jul 17, 2025 am 04:16 AM

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

How to handle File Uploads securely in PHP? How to handle File Uploads securely in PHP? Jul 08, 2025 am 02:37 AM

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

Commenting Out Code in PHP Commenting Out Code in PHP Jul 18, 2025 am 04:57 AM

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

How Do Generators Work in PHP? How Do Generators Work in PHP? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

Tips for Writing PHP Comments Tips for Writing PHP Comments Jul 18, 2025 am 04:51 AM

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

Quick PHP Installation Tutorial Quick PHP Installation Tutorial Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

Learning PHP: A Beginner's Guide Learning PHP: A Beginner's Guide Jul 18, 2025 am 04:54 AM

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

See all articles