search
HomeBackend DevelopmentPHP ProblemHow to convert string to array in php?

How to convert string to array in php?

Nov 05, 2020 pm 02:57 PM
phpstringarray

In PHP, you can use the built-in function explode() to convert a string into an array; this function can split a string into several substrings based on delimiters, and then combine these substrings into an array And return, the syntax format is "explode (delimiter, string, return the number of array elements)".

"How

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

Recommended: "PHP Video Tutorial"

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

explode(separator,string,limit)

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 remaining part of string;

    • If limit is not If it is empty and negative, all elements except the last limit elements will be 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 delimiter The contained value is not found in string and a negative limit is used, an empty array is returned, otherwise an array containing a single element of string is returned.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$str = &#39;PHP中文网,PHP教程,https://www.php.cn/,explode()函数,字符串转数组&#39;;
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
$arr = explode(&#39;,&#39;, $str);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, 3);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, -2);
print_r($arr);
$arr = explode(&#39;,&#39;, $str, 0);
print_r($arr);
$arr = explode(&#39;&#39;, $str);
var_dump($arr);
$arr = explode(&#39;@&#39;, $str, -1);
print_r($arr);
?>

Output:

"How

More programming related knowledge, Please visit: Programming Learning Course! !

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software