Home  >  Article  >  Backend Development  >  How to convert string into character array in php

How to convert string into character array in php

青灯夜游
青灯夜游Original
2022-02-09 17:13:242852browse

In PHP, you can use the str_split() function to convert a string into a character array. This function can split the string according to the specified length and pass it into the array. When the split length is 1, it can be Convert to character array; syntax "str_split(string,1)" or "str_split(string)".

How to convert string into character array in php

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

In PHP, when it comes to converting strings to characters For arrays, the explode() function comes to mind.

But the explode() function will split the string into several parts according to specific delimiters. It does not pass the string into the array character by character; the array element is not a single character, but a sub-character. string.

So howHow to convert a string into a character array in php?

In PHP, you can use the str_split() function to convert a string into a character array.

str_split() function splits a string into an array.

Syntax:

str_split(string,length)
  • string Required. Specifies the string to be split.

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

Return value:

If length is less than 1, the str_split() function will return FALSE. If length is greater than the length of the string, the entire string will be returned as the only element of the array.

Example:

<?php
header("Content-type:text/html;charset=gdk");
var_dump(str_split("Hello"));
var_dump(str_split("Hello",1));
var_dump(str_split("Hello",2));
?>

How to convert string into character array in php

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to convert string into character array in php. 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