Skip to content

Instantly share code, notes, and snippets.

@tonylegrone
Created June 30, 2011 16:11
Show Gist options
  • Save tonylegrone/1056564 to your computer and use it in GitHub Desktop.
Save tonylegrone/1056564 to your computer and use it in GitHub Desktop.
Prints a formatted array from a per line list
<?php
/**
* prints a formatted array from a per line list
*
* Each value is converted to it's own key with
* spaces replaced with underscores and strtolower().
* Values are converted to uppercase first letters with ucwords().
*/
function list_to_array($string) {
$string = explode("\n",$string);
foreach ($string as $val) {
$val = trim($val);
$patterns = array('/[^a-zA-Z0-9-\s]/', '/ /');
$replacements = array('', '_');
$key = strtolower(preg_replace($patterns, $replacements, $val));
$string_processed[$key] = ucwords($val);
}
echo '<pre>';
var_export($string_processed);
echo '</pre>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment