Viewed   93 times

I was wondering if anyone could point me to a resource where the details of a serialized php string is documented. I would basically like to know the format/structure so I can write a function in VB.NET to serialize/deserialize it back.

Thanks!

 Answers

3

The basic structure is as follows:

Scalar types:

  1. Booleans are serialized as:

    b:<i>;
    

    where <i> is an integer with a value of either 0 (false) or 1 (true).

  2. Integers are serialized as:

    i:<i>;
    

    where <i> is the integer value.

  3. Floats are serialized as (with d meaning double):

    d:<f>;
    

    where <f> is the float value.

  4. Strings are serialized as:

    s:<i>:"<s>";
    

    where <i> is an integer representing the string length of <s>, and <s> is the string value.

Special types:

  1. null is simply serialized as:

    N;
    

Compound types:

  1. Arrays are serialized as:

    a:<i>:{<elements>}
    

    where <i> is an integer representing the number of elements in the array, and <elements> zero or more serialized key value pairs:

    <key><value>
    

    where <key> represents a serialized scalar type, and <value> any value that is serializable.

  2. Objects are serialized as:

    O:<i>:"<s>":<i>:{<properties>}
    

    where the first <i> is an integer representing the string length of <s>, and <s> is the fully qualified class name (class name prepended with full namespace). The second <i> is an integer representing the number of object properties. <properties> are zero or more serialized name value pairs:

    <name><value>
    

    where <name> is a serialized string representing the property name, and <value> any value that is serializable.

    There's a catch with <name> though:

    <name> is represented as

    s:<i>:"<s>";
    

    where <i> is an integer representing the string length of <s>. But the values of <s> differs per visibility of properties:

    a. With public properties <s> is the simple name of the property.

    b. With protected properties, however, <s> is the simple name of the property, prepended with * — an asterix, enclosed in two NUL characters (i.e. chr(0)).

    c. And with private properties, <s> is the simple name of the property, prepended with <s><s>, enclosed in two NUL characters, where <s> is the fully qualified class name.


There are a few other cases, such as R:<i>;, that represents references, that I haven't mentioned here (because I honestly haven't figured out the exact workings of it yet), but this should give you a decent idea about PHP's serializing mechanism.

Friday, September 2, 2022
1

There's no real reason to use regexes here, you can use string and array functions instead.

You can explode the part after the ? (which you can get using substr to get a substring and strrpos to get the position of the last ?) into an array, and use unset to remove arg3, and then join to put the string back together.:

$string = "http://domain.com/php/doc.php?arg1=0&arg2=1&arg3=0";
$pos = strrpos($string, "?"); // get the position of the last ? in the string
$query_string_parts = array();

foreach (explode("&", substr($string, $pos + 1)) as $q)
{
  list($key, $val) = explode("=", $q);
  if ($key != "arg3")
  {
    // keep track of the parts that don't have arg3 as the key
    $query_string_parts[] = "$key=$val";
  }
}

// rebuild the string
$result = substr($string, 0, $pos + 1) . join($query_string_parts);

See it in action at http://www.ideone.com/PrO0a

Friday, November 11, 2022
4

You need a Ajax call to pass the JS value into php variable

JS Code will be (your js file)

var jsString="hello";
$.ajax({
    url: "ajax.php",
    type: "post",
    data: jsString
});

And in ajax.php (your php file) code will be

$phpString = $_POST['data'];     // assign hello to phpString 
Thursday, November 10, 2022
 
2
$edited = stristr($sentence, $word);
$edited = str_ireplace($word, '<span class="found">'.$word.'</span>', $edited);

Demo.


Or better yet (preserves the original case and only bolds whole $words):

$edited = stristr($sentence, $word);
$edited = preg_replace('~b(' . preg_quote($word, '~') . ')b~i', '<span class="found">$1</span>', $edited);

Another demo.

Saturday, August 27, 2022
 
rtigger
 
1

You could use the JSON format that is already implemented in several languages.

Saturday, September 10, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :