php get java properties fix(utf-8)

$properties = parse_properties ( 'prop.properties' );
var_export ( $properties );
function unicode2utf8($str) {
	if (! $str)
		return $str;
	$decode = json_decode ( $str );
	if ($decode)
		return $decode;
	$str = '["' . $str . '"]';
	$decode = json_decode ( $str );
	if (count ( $decode ) == 1) {
		return $decode [0];
	}
	return $str;
}
function parse_properties($propertiespath) {
	$txtProperties = file_get_contents ( $propertiespath );
	$result = array ();
	$lines = split ( "n", $txtProperties );
	$key = "";
	$isWaitingOtherLine = false;
	foreach ( $lines as $i => $line ) {
		if (empty ( $line ) || (! $isWaitingOtherLine && strpos ( $line, "#" ) === 0))
			continue;

		if (! $isWaitingOtherLine) {
			$key = substr ( $line, 0, strpos ( $line, '=' ) );
			$value = substr ( $line, strpos ( $line, '=' ) + 1, strlen ( $line ) );
		} else {
			$value .= $line;
		}
		/* Check if ends with single '' */
		if (strrpos ( $value, "" ) === strlen ( $value ) - strlen ( "" )) {
			$value = substr ( $value, 0, strlen ( $value ) - 1 ) . "n";
			$isWaitingOtherLine = true;
		} else {
			$isWaitingOtherLine = false;
		}
		$value = trim ( $value );
		$value = preg_replace ( "/(\uw{4})/e", "unicode2utf8('1')", $value );
		$value = preg_replace ( "/\\/", "", $value );
		$value = preg_replace ( "/\:/", ":", $value );
		$result [$key] = $value;
		unset ( $lines [$i] );
	}
	return $result;
}

转发请注明出处http://blog.martoo.cn
如有漏缺,请联系我 QQ 243008827

发表评论

电子邮件地址不会被公开。 必填项已用*标注