Skip to content

Instantly share code, notes, and snippets.

@alecgorge
Created May 18, 2011 00:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save alecgorge/977771 to your computer and use it in GitHub Desktop.
Save alecgorge/977771 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;
}
@Mr-Khan
Copy link

Mr-Khan commented Mar 28, 2018

change split to explode as The PHP manual for split() says

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged...Use explode() instead.

and
change $result[$key] = $value; to if($key != "") $result[$key] = $value; as blank lines will add an empty array value

@koromerzhin
Copy link

I write a new version of this parse properties :

https://gist.github.com/koromerzhin/84a1100777124932725b2b37217529b0

@dev-mansonthomas
Copy link

I write a new version of this parse properties :

https://gist.github.com/koromerzhin/84a1100777124932725b2b37217529b0

No longer available

@gabrielkiss
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment