Home >Backend Development >PHP Tutorial >Summary of frequently asked questions about php string operations
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"}
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);
The program will print out:
{"name":"Eric","age":23}
##
$array = array(0 => "Eric", 1 => 23); echo json_encode($array);
["Eric",23]
1. Convert string to array:
explode(separate,string)
Example:$str = "Hello world It's a beautiful day"; explode(" ",$str);//以空格为分界点
array([0]=>"Hello",[1]=>"world",[2]=>"It's",[3]=>"a",[4]=>"beautiful",[5]=>"day")
##Return the serialized string to the original array form.
##implode(separate,array) //The reverse operation of explode, separate defaults to the empty character
Example:
$array = ('hello','world','!'); implode(" ",$array);
Return:
"hello world !"
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Usage of foreach in PHP
PHP imports Excel files into MySQL database
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!