You are here

function node_export_remove_recursion in Node export 6.2

Same name and namespace in other branches
  1. 6.3 node_export.module \node_export_remove_recursion()
  2. 7.3 node_export.module \node_export_remove_recursion()

Remove recursion problem from an object or array.

1 call to node_export_remove_recursion()
node_export_node_export in ./node_export.module
Exports a node - populate a node code form set $return_code to TRUE to not return form but the code instead. set $format to some string if encoding should be handled by some module that will recognise the string.

File

./node_export.module, line 992
The Node Export module.

Code

function node_export_remove_recursion($o) {
  static $replace;
  if (!isset($replace)) {
    $replace = create_function('$m', '$r="\\x00{$m[1]}ecursion_export_node_";return \'s:\'.strlen($r.$m[2]).\':"\'.$r.$m[2].\'";\';');
  }
  if (is_array($o) || is_object($o)) {
    $re = '#(r|R):([0-9]+);#';
    $serialize = serialize($o);
    if (preg_match($re, $serialize)) {
      $last = $pos = 0;
      while (false !== ($pos = strpos($serialize, 's:', $pos))) {
        $chunk = substr($serialize, $last, $pos - $last);
        if (preg_match($re, $chunk)) {
          $length = strlen($chunk);
          $chunk = preg_replace_callback($re, $replace, $chunk);
          $serialize = substr($serialize, 0, $last) . $chunk . substr($serialize, $last + ($pos - $last));
          $pos += strlen($chunk) - $length;
        }
        $pos += 2;
        $last = strpos($serialize, ':', $pos);
        $length = substr($serialize, $pos, $last - $pos);
        $last += 4 + $length;
        $pos = $last;
      }
      $serialize = substr($serialize, 0, $last) . preg_replace_callback($re, $replace, substr($serialize, $last));
      $o = unserialize($serialize);
    }
  }
  return $o;
}