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

Summary of frequently asked questions about php string operations

墨辰丷
墨辰丷Original
2018-06-01 10:09:161512browse

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);


The program will print out:


["Eric",23]


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);//以空格为分界点


Return:


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


##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);


Return:

"hello world !"

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!

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