Hàm parse_ini_string() sẽ lấy thông tin cấu hình từ chuỗi cấu hình truyền vào..
parse_ini_string ( string$ini[, bool$process_sections=FALSE[, int$scanner_mode= INI_SCANNER_NORMAL ]] ) : array
$ini là chuỗi ini cần lấy thông tin.$process_sections là tham số, mặc định là False, nếu mang giá trị là True, bạn sẽ có một mảng đa chiều bao gồm tên phần tử và các thiết lập.$scanner_mode có thể là INI_SCANNER_NORMAL (mặc định) hoặc INI_SCANNER_RAW. Nếu mang giá trị INI_SCANNER_RAW, thì các giá trị tùy chọn sẽ không được phân tích cú pháp.<?php
if ( !function_exists( 'parse_ini_string' ) ) {
function parse_ini_string( $string, $process_sections ) {
if ( !class_exists( 'parse_ini_filter' ) ) {
/* Define our filter class */
class parse_ini_filter extends php_user_filter {
static $buf = '';
function filter( $in, $out, &$consumed, $closing ) {
$bucket = stream_bucket_new( fopen('php://memory', 'wb'), self::$buf );
stream_bucket_append( $out, $bucket );
return PSFS_PASS_ON;
}
}
/* Register our filter with PHP */
stream_filter_register("parse_ini", "parse_ini_filter")
or return false;
}
parse_ini_filter::$buf = $string;
return parse_ini_file( "php://filter/read=parse_ini/resource=php://memory", $process_sections );
}
}
?><?php function parse_ini_string_m($str) { if(empty($str)) return false; $lines = explode("\n", $str); $ret = Array(); $inside_section = false; foreach($lines as $line) { $line = trim($line); if(!$line || $line[0] == "#" || $line[0] == ";") continue; if($line[0] == "[" && $endIdx = strpos($line, "]")){ $inside_section = substr($line, 1, $endIdx-1); continue; } if(!strpos($line, '=')) continue; $tmp = explode("=", $line, 2); if($inside_section) { $key = rtrim($tmp[0]); $value = ltrim($tmp[1]); if(preg_match("/^\".*\"$/", $value) || preg_match("/^'.*'$/", $value)) { $value = mb_substr($value, 1, mb_strlen($value) - 2); } $t = preg_match("^\[(.*?)\]^", $key, $matches); if(!empty($matches) && isset($matches[0])) { $arr_name = preg_replace('#\[(.*?)\]#is', '', $key); if(!isset($ret[$inside_section][$arr_name]) || !is_array($ret[$inside_section][$arr_name])) { $ret[$inside_section][$arr_name] = array(); } if(isset($matches[1]) && !empty($matches[1])) { $ret[$inside_section][$arr_name][$matches[1]] = $value; } else { $ret[$inside_section][$arr_name][] = $value; } } else { $ret[$inside_section][trim($tmp[0])] = $value; } } else { $ret[trim($tmp[0])] = ltrim($tmp[1]); } } return $ret; } ?> <?php $ini = ' [simple] val_one = "some value" val_two = 567 [array] val_arr[] = "arr_elem_one" val_arr[] = "arr_elem_two" val_arr[] = "arr_elem_three" [array_keys] val_arr_two[6] = "key_6" val_arr_two[some_key] = "some_key_value" '; $arr = parse_ini_string_m($ini); ?>Kết quả có thể là.
Array
(
[simple] => Array
(
[val_one] => some value
[val_two] => 567
)
[array] => Array
(
[val_arr] => Array
(
[0] => arr_elem_one
[1] => arr_elem_two
[2] => arr_elem_three
)
)
[array_keys] => Array
(
[val_arr_two] => Array
(
[6] => key_6
[some_key] => some_key_value
)
)
)Các bạn có thể xem chi tiết hơn trên php.net.
Hi vọng với bài viết này, bạn đã hiểu rõ ứng dụng của hàm parse_ini_string() trong PHP. Nếu bạn thấy bài viết hay và có ý nghĩa hãy like và chia sẻ bài viết này để mọi người cùng nhau học tập nhé. Cảm ơn các bạn đã ghé thăm codetutam.com
Bình luận: