PHP String Manipulation FAQ Summary _php Tips

Source: Internet
Author: User
Tags explode learn php

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:

["Eric", 23]

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:

"Hello World!"

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.