Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common problems in php string manipulation?

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report