Created
May 18, 2011 00:29
-
-
Save alecgorge/977771 to your computer and use it in GitHub Desktop.
Parse Java properties files in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function parse_properties($txtProperties) { | |
$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; | |
} | |
$result[$key] = $value; | |
unset($lines[$i]); | |
} | |
return $result; | |
} |
I write a new version of this parse properties :
https://gist.github.com/koromerzhin/84a1100777124932725b2b37217529b0
No longer available
I too wrote my version of this parser https://gist.github.com/gabrielkiss/1c67f632a596e7acd288b7c7c751df98
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I write a new version of this parse properties :
https://gist.github.com/koromerzhin/84a1100777124932725b2b37217529b0