The examples in this article describe the common problems with PHP string manipulation. Share to everyone for your reference, specific as follows:
Remember the first time to learn PHP to listen to the words of a cow x is: All procedures are strings, the so-called programming just let the data like water in the various code page flow between the flow. I do find that the data format is a difficult problem in my current job, involving the assembling, splitting and reassembly of data.
The reason to mention JSON is that when using AJAX, it often involves data interaction between programs and JS. Because JS does not know the array in PHP, PHP does not know the array or object in JS. This free format of JSON is a good solution to this problem.
Its format is as follows:
For example:
{"username": "Eric", "Age": N, "Sex": "Man"}
Our powerful PHP has already provided built-in functions for this: Json_encode () and Json_decode ().
It's easy to understand that Json_encode () is converting a PHP array into JSON. Instead, Json_decode () converts JSON into a PHP array.
For example:
$array = Array ("name" => "Eric", "Age" =>);
echo Json_encode ($array);
Program will print out:
{' name ': ' Eric ', ' age ': 23}
$array = Array (0 => "Eric", 1 =>);
echo Json_encode ($array);
Program will print out:
In addition to this relatively free format, the more common is the interchange and concatenation between strings and arrays:
1. Convert string to array:
Explode (separate,string)
Example:
$STR = "Hello World It's a Beautiful Day";
Explode ("", $str);//space as the dividing point
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 an array to a string:
Implode (Separate,array)//explode reverse operation, separate default is null character
Example:
$array = (' Hello ', ' world ', '! ');
Implode ("", $array);
Return:
More interested in PHP related content readers can view the site topics: "PHP string (String) Usage summary", "PHP Array" Operation Techniques Encyclopedia, "PHP Basic Grammar Primer", "PHP Operations and Operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "Php+mysql Database Operation Introduction" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.