Use stringtree PHP to remotely transmit data

Source: Internet
Author: User

The HTML protocol is text-based, so the transmission of text data is relatively reliable. More importantly, it is very troublesome for JavaScript and PHP to process binary data, therefore, transferring all types of data using text is a preferred method.

In most cases, we need to pass complicated content. These texts must be parsed before they can be used. Although the parsing method depends on specific applications, the most commonly used array is the dictionary format, that is, the key = value method, such as parameter transmission. more complex, multi-level data may be required. tree data is the most widely used scenario. Here we can package and parse tree data. note that if Ajax is used to transmit data,
Encode the data.

Class stringtree {var $ value; var $ child; function _ construct ($ name = "stringtree") {$ this-> value = $ name; $ this-> child = array ();} private function _ tostr (& $ Str) {$ Str. = addcslashes ($ this-> value ,"{}"). "{"; foreach ($ this-> child as & $ Val) {$ Val-> _ tostr ($ Str) ;}$ Str. = "}" ;}function getdiction ($ SP) {$ A = array (); $ slen = strlen ($ SP); if ($ slen = 0) return; $ COUNT = count ($ this-> child); For ($ I = 0; $ I <$ count; $ I ++) {$ v = $ This-> child [$ I]-> value; $ Pos = strpos ($ V, $ SP); if ($ Pos =-1) {$ A [$ v] = "" ;}else {$ key = substr ($ V, 0, $ POS); $ val = substr ($ V, $ POS + $ slen); $ A [$ key] = $ Val ;}return $ A ;}// represents an object as a string. Function tostr () {$ STR = ""; $ this-> _ tostr ($ Str); return $ STR ;}// this function replaces the stripcslashes function of the system, because the latter cannot remove the backslash before those characters. private function _ stripcslashes ($ STR, $ chars) {$ Len = strlen ($ chars); $ patern = array (); $ Rp = array (); for ($ I = 0; $ I <$ Len; $ I ++) {$ patern [$ I] = "/\\\\". $ chars [$ I]. "/"; $ RP [$ I] = $ chars [$ I];} return preg_replace ($ patern, $ RP, $ Str );} private function _ fromstr (& $ STR, & $ offset, $ Len) {for ($ I = $ offset; $ I <$ Len; $ I +) {If ($ STR [$ I] = '{' & $ STR [$ I-1]! = '\\') {$ Name = substr ($ STR, $ offset, $ I-$ offset); $ name = $ this-> _ stripcslashes ($ name, "{}"); $ offset = $ I + 1; $ CST = $ this-> addchild ($ name); // echo $ name. "<br/>"; $ CST-> _ fromstr ($ STR, $ offset, $ Len); $ I = $ offset-1; // After _ fromstr is executed, offset is either the "}" Position + 1 or $ Len, because the For Loop will automatically + 1. In this case, you need to roll back 1 to avoid skipping a character .} elseif ($ STR [$ I] = '}' & $ STR [$ I-1]! = '\\') {$ Offset = $ I + 1; return ;}}$ offset = $ Len;} function fromstr ($ Str) {$ this-> child = array (); $ this-> value = ""; $ offset = 0; $ Len = strlen ($ Str ); for ($ I = $ offset; $ I <$ Len; $ I ++) {if ($ STR [$ I] = '{' & ($ I <= 0 | $ STR [$ I-1]! = '\') {$ Name = substr ($ STR, $ offset, $ I-$ offset); $ this-> value = stripcslashes ($ name ); $ offset = $ I + 1; $ this-> _ fromstr ($ STR, $ offset, $ Len); $ I = $ offset-1 ;} elseif ($ STR [$ I] = '}' & ($ I <= 0 | $ STR [$ I-1]! = '\') {Break ;}}// Add a subnode. The returned value is the newly added node function addchild ($ Val) {$ I = count ($ this-> child); $ this-> child [$ I] = new stringtree ($ Val ); return $ this-> child [$ I];} // return the number of subnodes function count () {return count ($ this-> child);} function clear () {$ this-> child = array () ;};/* usage instructions: The stringtree class has the following functions: 1. stringtree (Root) constructor. The parameter is the root name of the Data Tree. 2. addchild (value) adds data. The parameter is a string and the data we need to save. the return value of this function is also a stringtree object, which is the sub-object of its parent object. 3. returns the string representing the entire data tree. 4. fromstr (STR) generates the data of the stringtree object from a string. The data before this object will be cleared. This string is generally the string returned by the tostr () function, and can also be constructed by itself, it is very simple to construct rules. 5. clear () clears all sub-object data of the Data tree, but the root data is not cleared. 6. count () returns the number of sub-objects in the data tree. 7. getdiction (SP) returns a dictionary array that represents the subdata of the current data tree object, but does not contain the next set of subdata. because, in fact, each data is a string, an SP separator is required to specify the key and value. For example, we use addchild ("Name: Zhang San"); this statement adds data, set SP to ":". The array returned by the getdiction function will have an element such as DIC ["name"], and its value is "Zhang San ". if the key field is repeated when I add data, for example, addchild ("Name: Zhang San"); addchild ("Name: Li Si, when getdiction is used, only one data entry is obtained. The preceding data entry is overwritten and only the added data is returned. each stringtree object has two member variables, value and child. The former is the string value we added using addchild. Therefore, we can modify this value to modify the data. echo addchild ("Name: James")-> value; the output will be "Name: James ". the child member is a child stringtree array, that is, the Child Branch data of the parent object. the string value output by the tostr () of the stringtree object is in the following format: Root {Child1 {sonchild1 {}} child2 {} Child3 {}}, that is, each data is separated by a {}. If it contains sub-data, it should be written in {}, and {} should be added at the end {}. you can manually construct a string that complies with this rule and assign it to fromstr to generate the stringtree object tree. fromstr may partially parse an invalid string, but the function will not return an error. although {} is used as the separator, the data added by addchild or the string operated directly using value does not need to be considered whether it contains the "{" or "}" symbol, tostr () and fromstr () functions have been escaped. however, when manually constructing the string of the stringtree data tree, you need to add "\" before "{" and "}" to escape. */$ ST = new stringtree (); $ St-> addchild ("Name: phpstringtree"); $ CH2 = $ St-> addchild ("Child: phpchild2 "); $ St-> addchild ("value: phpstring"); $ CH3 = $ CH2-> addchild ("AB {c} D"); echo $ CH3-> value. "<br/>"; $ STR = $ St-> tostr (); echo $ Str. "<br/>"; $ ST = new stringtree (); $ St-> fromstr ($ Str ); echo $ St-> child [1]-> child [0]-> value. "<br/>"; $ A = $ St-> getdiction (":"); print_r ($ );

Output:

AB {c} d
Stringtree {Name: phpstringtree {} Child: phpchild2 {AB \ {c \} d {}} value: phpstring {}}
AB {c} d
Array ([name] => phpstringtree [child] => phpchild2 [value] => phpstring)

Javascript version:

// Functions of the same name as PHP functions. If a letter in the STR string is contained in the Chars string, a backslash is added. function addcslashes (STR, chars) {chars = '([' + chars + '])'; var Reg = Regexp (chars, "G"); Return Str. replace (Reg, '\ $1');} // remove the backslash before the specified character and the inverse function of addcslashes function. function stripcslashes (STR, chars) {chars = "(\\\\) ([" + chars + "])"; var Reg = Regexp (chars, "G "); return Str. replace (Reg, "$2");} function stringtree (STR) {This. value = STR ;//. replace (, ); This. child = new array (); // This function is used to save a value into a dictionary based on the delimiter sp, and then you can conveniently retrieve a specific value this. getdiction = function (SP) {var A = new array (); For (VAR I = 0; I <this. child. length; I ++) {var v = This. child [I]. value; var Index = v. indexof (SP); var key, Val; If (Index =-1) {key = V; val = "" ;}else {key = v. substr (0, index); val = v. substr (index + sp. length);} A [Key] = val;} return a;} This. tostr = function () {var S = new array (); S. push (addcsl Ashes (this. value, "{}"); S. push ("{"); For (var I in this. child) {S. push (this. child [I]. tostr ();} s. push ("}"); Return S. join ("");} This. addchild = function (STR) {var ST = new stringtree (STR); this. child. push (ST); Return st;} This. _ fromstr = function (STR, offset, Len) {for (VAR I = offset; I <Len; I ++) {If (STR [I] = '{' & STR [I-1]! = '\') {Var name = Str. substr (offset, I-offset); name = stripcslashes (name, "{}"); offset = I + 1; var CST = This. addchild (name); offset = Cst. _ fromstr (STR, offset, Len); I = offset-1;} else if (STR [I] = '}' & STR [I-1]! = '\') {Offset = I + 1; return offset ;}} return Len;} This. fromstr = function (STR) {This. value = ""; var offset = 0; var Len = Str. length; For (VAR I = offset; I <Len; I ++) {If (STR [I] = '{' & (I <= 0 | STR [I-1]! = '\') {Var name = Str. substr (offset, I-offset); this. value = stripcslashes (name, "{}"); offset = I + 1; offset = This. _ fromstr (STR, offset, Len); I = offset-1 ;} else if (STR [I] = '}' & (I <= 0 | STR [I-1]! = '\') {Break ;}}} document. write ("<br/> javascript: <br/>"); var ST = new stringtree ("javascriptstringtree"); ST. addchild ("Name: javascriptstringtree"); var CH2 = ST. addchild ("Child: javascriptchild2"); ST. addchild ("value: javascriptstring"); ch2.addchild ("ABC"); var STR = ST. tostr (); document. write (STR + "<br/>"); ST = new stringtree (); ST. fromstr (STR); document. write (St. child [1]. child [0]. value + "<br/>"); var DIC = ST. getdiction (":"); document. write ("name:" + DIC ["name"] + "<br/>"); document. write ("Child:" + DIC ["child"] + "<br/>"); document. write ("value:" + DIC ["value"] + "<br/> ");

The functions, members, and usage of stringtree In the Javascript version are exactly the same as those in the PHP version, including case sensitivity.

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.