Home  >  Article  >  Backend Development  >  Can’t Chinese characters be placed in the php array?

Can’t Chinese characters be placed in the php array?

青灯夜游
青灯夜游Original
2022-08-22 17:00:061908browse

Chinese characters can be placed in the php array. Because PHP is a programming language with weak data types, arrays in PHP can store any number of data of any type, that is, there is no limit on the type of array elements, which can be numbers, Boolean values, arrays, strings, etc.; while strings It can be divided into pure English strings, pure Chinese strings and mixed Chinese and English strings.

Can’t Chinese characters be placed in the php array?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

Array is one of the most important data types in PHP First, it is widely used in PHP. Because PHP is a programming language with weak data types, array variables in PHP can store any number of data of any type, and can implement the functions of data structures such as heaps, stacks, and queues in other strong data types.

Simply put, There is no restriction on the type of PHP array elements, which can be numbers, strings, Boolean values, arrays, Object objects, etc.

A string is a continuous sequence of characters, consisting of a series of characters, where each character is equivalent to one byte.

Strings can be divided into pure English strings, pure Chinese strings and mixed Chinese and English strings. Therefore, Chinese characters can be placed in the php array.

Example 1: Put pure English strings in php array

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array(1,2,"hello",TRUE,[3,4]);
var_dump($arr);
?>

Can’t Chinese characters be placed in the php array?

Example 2: php array Put the pure Chinese string

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("你好","汤姆","hello",TRUE,[3,4]);
var_dump($arr);
?>

Can’t Chinese characters be placed in the php array?

. It should be noted that in PHP, one Chinese character occupies three characters, so the length of the two Chinese strings in the above example For 6.

Example 3: Put Chinese and English mixed strings in the php array

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("你好124","23汤姆","hello",TRUE,[3,4]);
var_dump($arr);
?>

Can’t Chinese characters be placed in the php array?

Extended knowledge: Chinese Two processing functions for strings

1. Chinese interception: mb_substr()

mb_substr( $str, $start, $length, $encoding )
  • $str, the string that needs to be truncated

  • $start, the starting point of truncation, the starting point is 0

  • ##$length, the number of words to be intercepted

  • $encoding, web page encoding, such as utf-8, GB2312, GBK

  • <?php
    header("Content-type:text/html;charset=utf-8"); 
    $str=&#39;php中文网://www.php.cn&#39;; 
    echo mb_substr($str,0,4,&#39;utf-8&#39;);//截取头5个字,假定此代码所在php文件的编码为utf-8 
    ?>

Can’t Chinese characters be placed in the php array?

2. Get the Chinese length: mb_strlen()

mb_strlen( $str, $encoding )

  • $str, the string whose length needs to be calculated

  • $encoding, web page encoding, such as utf-8, GB2312, GBK

  • <?php
    header("Content-type:text/html;charset=utf-8"); 
    $str=&#39;php中文网://www.php.cn&#39;; 
    echo mb_strlen($str,&#39;utf-8&#39;);//假定此代码所在php文件的编码为utf-8 
    ?>

Can’t Chinese characters be placed in the php array?

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of Can’t Chinese characters be placed in the php array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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