How to convert string to array in php
In the process of PHP programming, we often need to convert strings into arrays to process data more conveniently. Below we will introduce several common methods of converting PHP strings to arrays.
Method 1: Use the explode function
Using the explode function is the most commonly used method to convert a PHP string into an array. It can divide the string according to the specified delimiter and return an array.
Sample code:
$str = 'apple,orange,banana'; $arr = explode(',', $str); print_r($arr);
Output result:
Array ( [0] => apple [1] => orange [2] => banana )
Method 2: Use the str_split function
The str_split function can divide the string according to the specified length , and returns an array.
Sample code:
$str = 'hello world'; $arr = str_split($str); print_r($arr);
Output result:
Array ( [0] => h [1] => e [2] => l [3] => l [4] => o [5] => [6] => w [7] => o [8] => r [9] => l [10] => d )
Method 3: Use the preg_split function
The preg_split function can split the string according to the regular expression points and returns an array. This method is more flexible and can adjust the regular expression according to different situations.
Sample code:
$str = '1a2b3c4d5e'; $arr = preg_split('/\d/', $str); print_r($arr);
Output result:
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e )
Method 4: Use the str_replace function
The str_replace function can replace the specified characters in the string with another character and returns a new string. We can replace the character to be replaced with a comma, and then use the explode function to convert it into an array.
Sample code:
$str = '1-2-3-4-5'; $str = str_replace('-', ',', $str); $arr = explode(',', $str); print_r($arr);
Output result:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Method 5: Use json_decode function
If our string conforms to json format, then We can convert it to an array using the json_decode function.
Sample code:
$str = '{"name":"Tom","age":18}'; $arr = json_decode($str, true); print_r($arr);
Output result:
Array ( [name] => Tom [age] => 18 )
Summary
The above are several common methods of converting PHP strings to arrays. We can use Choose the most appropriate method for the actual situation. It is worth noting that when using the explode function or preg_split function to split a string, if the split result contains spaces or other invalid characters, filtering or processing is required to obtain the correct array.
The above is the detailed content of How to convert string to array in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

Prepared statements in PHP enhance database security and efficiency by preventing SQL injection and improving query performance through compilation and reuse.Character count: 159

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159
