Skip to content

Instantly share code, notes, and snippets.

@gsdefender
Forked from alecgorge/java-properties.php
Created October 27, 2015 15:02
Show Gist options
  • Save gsdefender/cd7ddd6f0d7e98d094a2 to your computer and use it in GitHub Desktop.
Save gsdefender/cd7ddd6f0d7e98d094a2 to your computer and use it in GitHub Desktop.
Parse Java properties files in PHP
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment