How to convert numeric string to array in php

青灯夜游
Release: 2023-03-08 07:02:02
Original
3512 people have browsed it

Conversion method: 1. Use explode() to split the string into several substrings according to specific delimiters, and then combine the substrings into an array; the syntax format is "explode(separator, string)". 2. Use str_split() to split the string into an array, the syntax format is "str_split(string)".

How to convert numeric string to array in php

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

numeric characters in php String to array

1. Use the explode() function

During the development process, we often encounter the need to convert a string into an array Case. PHP has a built-in explode() function that can split a string into several parts based on specific delimiters.

explode() function can split a string based on the string delimiter, that is, it splits a string into several substrings based on the delimiter, and then combines these substrings into an array and returns it. The syntax format is as follows:

explode($delimiter, $string [, $limit])
Copy after login

The parameter description is as follows:

  • $delimiter: the delimiter character used to split the string;

  • $string: The string that needs to be split;

  • $limit: Optional parameter, which can be empty, specifies the number of array elements to be returned;

    • If $limit is not empty and is a positive number, the returned array contains at most $limit elements, and the last element contains the remainder of $string;

    • If $limit is not empty and is negative, all elements except the last $limit elements are returned;

    • If $limit is 0, it will be treated as 1;

    • If $limit is empty, it means returning all array elements.

If $delimiter is an empty string"", the program will prompt a Warning, and the explode() function will return FALSE; if If the value contained in $delimiter is not found in $string, and a negative $limit is used, an empty array will be returned, otherwise an array containing a single element of $string will be returned.

<?php
    $str = &#39;544527258895525404524.54434245657&#39;;
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    $arr = explode(&#39;2&#39;, $str);
    print_r($arr);
    $arr = explode(&#39;2&#39;, $str, 3);
    print_r($arr);
    $arr = explode(&#39;2&#39;, $str, -2);
    print_r($arr);
    $arr = explode(&#39;2&#39;, $str, 0);
    print_r($arr);
    $arr = explode(&#39;&#39;, $str);
    var_dump($arr);
    $arr = explode(&#39;@&#39;, $str, -1);
    print_r($arr);
?>
Copy after login

Output result:

How to convert numeric string to array in php

[Recommended learning: "PHP Video Tutorial"]

2. Use the str_split() function

str_split() function to split the string into an array.

Syntax

str_split(string,length)
Copy after login

The parameter description is as follows:

  • 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 is returned as the only element of the array.

Example:

<?php
    $str = &#39;536546&#39;;
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    $arr = str_split($str);
    print_r($arr);
	$arr = str_split($str,3);
    print_r($arr);
?>
Copy after login

Output result:

How to convert numeric string to array in php

##For more programming related knowledge, please visit:

Programming Video ! !

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