What are the common problems in php string manipulation?
This article mainly shows you "php string operation common problems", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "what are common problems with php string operation" this article.
The details are as follows:
I remember a sentence I heard when I was learning php that is: all programs are strings, and the so-called programming is just to let data flow back and forth between code pages like water. In my current work, I do find that data format is a very difficult problem, which involves the assembly, disassembly and reassembly of data.
Json is mentioned because when using ajax, it often involves the data interaction between the program and Js. Because Js does not know 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 already provides built-in functions for this: json_encode () and json_decode ().
It's easy to understand that json_encode () translates the PHP array into Json. Instead, json_decode () converts Json to an array of PHP.
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)
The program will print out:
["Eric", 23]
In addition to this relatively free format, it is more common to interchange and concatenate strings and arrays:
1. Convert a string into an array:
Explode (separate,string)
Example:
$str = "Hello world It's a beautiful day"; explode ("", $str); / / demarcated by spaces
Return:
Array ([0] = > "Hello", [1] = > "world", [2] = > "It's", [3] = > "a", [4] = > "beautiful", [5] = > "day")
Returns the serialized string to the original array form.
2. Convert the array to a string:
The reverse operation of implode (separate,array) / / explode, which defaults to null characters
Example:
$array = ('Helllogged grammatical, worldwatch, array,'); implode (", $array)
Return:
"hello world!" The above is all the contents of the article "what are the common problems with php string manipulation?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!