How can I port PHP preg_split to Java for the special case of unserializing a value in ADODB?

半世苍凉 提交于 2019-12-10 22:41:36

问题


I need to port this function for unserializing a value in ADODB to Java.

        $variables = array( );
        $a = preg_split( "/(\w+)\|/", $serialized_string, -1, PREG_SPLIT_NO_EMPTY |   PREG_SPLIT_DELIM_CAPTURE );
        for( $i = 0; $i < count( $a ); $i = $i+2 ) {
            $variables[$a[$i]] = unserialize( $a[$i+1] );
        }

I have a library to unserialize the values the php way, but I need help on porting over the preg_split. What would this regex look like in Java?


回答1:


Equivalent java code :


import java.util.List;
import java.util.ArrayList;

// Test
String serialized_string = "foo|bar|coco123||cool|||";

// Split the test
String[] raw_results=serialized_string.split("\\|");// Trailing empty strings are removed but not internal ones

// Cleansing of the results
List<String> php_like_results = new ArrayList<String>();
for(String tmp : raw_results) {
    if (tmp.length()>0) {
       php_like_results.add(tmp);
    }
}

// Output results
System.out.println(php_like_results);

This will produce : [foo, bar, coco123, cool]



来源:https://stackoverflow.com/questions/4550772/how-can-i-port-php-preg-split-to-java-for-the-special-case-of-unserializing-a-va

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!