Home > Backend Development > PHP Tutorial > Summary of frequently asked questions about php string operations

Summary of frequently asked questions about php string operations

墨辰丷
Release: 2023-03-28 21:06:02
Original
1518 people have browsed it

This article mainly introduces the common problems of PHP string operations, and analyzes PHP's json operations and string conversion problems in the form of examples. Friends in need can refer to it

Remember when I first learned PHP One sentence I heard that I thought was awesome is: All programs are strings. The so-called programming is just letting data flow like water between various code pages. In my current work, I have indeed found that data format is a difficult problem, involving the assembly, splitting and reassembly of data.

The reason why Json is mentioned is because when using ajax, data interaction between the program and Js is often involved. Since JS does not recognize arrays in PHP, PHP does not recognize arrays or objects in JS. At this time, the free format of Json can solve this problem very well.

Its format is as follows:

For example:


{"username": "Eric","age":23,"sex": "man"}
Copy after login


Our powerful PHP has been used for This provides built-in functions: json_encode() and json_decode().

It is easy to understand, json_encode() converts a PHP array into Json. On the contrary, json_decode() converts Json into a PHP array.

For example:


$array = array("name" => "Eric","age" => 23);
echo json_encode($array);
Copy after login


The program will print out:


{"name":"Eric","age":23}
Copy after login



##

$array = array(0 => "Eric", 1 => 23);
echo json_encode($array);
Copy after login


The program will print out:


["Eric",23]
Copy after login


In addition to this relatively free format, the more common one is the exchange and splicing between strings and arrays:

1. Convert string to array:

explode(separate,string)

Example:


$str = "Hello world It's a beautiful day";
explode(" ",$str);//以空格为分界点
Copy after login


Return:


array([0]=>"Hello",[1]=>"world",[2]=>"It's",[3]=>"a",[4]=>"beautiful",[5]=>"day")
Copy after login


##Return the serialized string to the original array form.

2. Convert the array into a string:

##implode(separate,array) //The reverse operation of explode, separate defaults to the empty character

Example:

$array = ('hello','world','!');
implode(" ",$array);
Copy after login


Return:

"hello world !"
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Usage of foreach in PHP


PHP imports Excel files into MySQL database

How to use

php

code in smarty template language

The above is the detailed content of Summary of frequently asked questions about php string operations. 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