Home > Backend Development > PHP Problem > How to convert php string to array bitwise

How to convert php string to array bitwise

青灯夜游
Release: 2023-03-15 17:34:02
Original
2583 people have browsed it

In PHP, you can use the str_split() function to convert a string into an array bitwise. This function can split the string into an array. You only need to omit the second parameter, or set the value to "1 ", you can split the string, split the string into characters one by one, and pass it into the array bit by bit; the syntax is "str_split("string")".

How to convert php string to array bitwise

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use str_split () function converts a string into an array bitwise.

The str_split() function can split a string into an array.

This function supports two parameters, the syntax is

str_split(string,length)
Copy after login
  • string: required. Specifies the string to be split.

  • #length: Optional. Specifies the length of each array element. The default is 1.

When you only need to omit the second parameter, or set the value to "1", you can split the string, split the string into characters one by one, and pass it in bit by bit In an array, as an array element.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str= "Hello";
$arr=str_split($str);
var_dump($arr);
$arr=str_split($str,1);
var_dump($arr);
?>
Copy after login

How to convert php string to array bitwise

Extended knowledge:

str_split() function cannot separate Chinese characters The problem can be solved by using the following code:

<?php
header("Content-type:text/html;charset=utf-8");
function mb_str_split($str,$split_length=1,$charset="UTF-8"){
  if(func_num_args()==1){
    return preg_split(&#39;/(?<!^)(?!$)/u&#39;, $str);
  }
  if($split_length<1)return false;
  $len = mb_strlen($str, $charset);
  $arr = array();
  for($i=0;$i<$len;$i+=$split_length){
    $s = mb_substr($str, $i, $split_length, $charset);
    $arr[] = $s;
  }
  return $arr;
}
var_dump(mb_str_split(&#39;Hello&#39;));
var_dump(mb_str_split(&#39;PHP中文网&#39;));
?>
Copy after login

How to convert php string to array bitwise

Recommended: "PHP Video Tutorial"

The above is the detailed content of How to convert php string to array bitwise. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template