| 260 | | // Code from symfony sfToolkit class. See LICENSE |
| 261 | | /** |
| 262 | | * stringToArray |
| 263 | | * |
| 264 | | * @param string $string |
| 265 | | * @return void |
| 266 | | */ |
| 267 | | public static function stringToArray($string) |
| 268 | | { |
| 269 | | preg_match_all('/ |
| 270 | | \s*(\w+) # key \\1 |
| 271 | | \s*=\s* # = |
| 272 | | (\'|")? # values may be included in \' or " \\2 |
| 273 | | (.*?) # value \\3 |
| 274 | | (?(2) \\2) # matching \' or " if needed \\4 |
| 275 | | \s*(?: |
| 276 | | (?=\w+\s*=) | \s*$ # followed by another key= or the end of the string |
| 277 | | ) |
| 278 | | /x', $string, $matches, PREG_SET_ORDER); |
| 279 | | |
| 280 | | $attributes = array(); |
| 281 | | foreach ($matches as $val) { |
| 282 | | $attributes[$val[1]] = self::literalize($val[3]); |
| 283 | | } |
| 284 | | |
| 285 | | return $attributes; |
| 286 | | } |
| 287 | | |
| 288 | | /** |
| 289 | | * Finds the type of the passed value, returns the value as the new type. |
| 290 | | * |
| 291 | | * @param string |
| 292 | | * @return mixed |
| 293 | | */ |
| 294 | | public static function literalize($value, $quoted = false) |
| 295 | | { |
| 296 | | // lowercase our value for comparison |
| 297 | | $value = trim($value); |
| 298 | | $lvalue = strtolower($value); |
| 299 | | |
| 300 | | if (in_array($lvalue, array('null', '~', ''))) |
| 301 | | { |
| 302 | | $value = null; |
| 303 | | } else if (in_array($lvalue, array('true', 'on', '+', 'yes'))) { |
| 304 | | $value = true; |
| 305 | | } else if (in_array($lvalue, array('false', 'off', '-', 'no'))) { |
| 306 | | $value = false; |
| 307 | | } else if (ctype_digit($value)) { |
| 308 | | $value = (int) $value; |
| 309 | | } else if (is_numeric($value)) { |
| 310 | | $value = (float) $value; |
| 311 | | } else { |
| 312 | | if ($quoted) |
| 313 | | { |
| 314 | | $value = '\''.str_replace('\'', '\\\'', $value).'\''; |
| 315 | | } |
| 316 | | } |
| 317 | | |
| 318 | | return $value; |
| 319 | | } |
| 320 | | |