01c05-1.php
来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 36 行
PHP
36 行
<?php// Generate a function to parse CSV strings,// with configurable delimiters and quote charactersfunction parse_csv($string, $delim = ',', $quote='"') { // First clean up any double-quotes, our algorithm doesn't need them: $new = str_replace("{$quote}{$quote}", $quote, $string); // Grab all matches $matches = array(); preg_match_all("/\s*({$quote}?)(.*?)\\1\s*(?:{$delim}|$)/", $new, $matches); // matches[2] holds our answers but will always grab one too many // matches with the final match being blank. Remove it then return: array_pop($matches[2]); return $matches[2];}$str = 'comma, "separated ""values""", are , "shown, here", "for\nyou " ';// Parse the string into values:$values = parse_csv($str);// Echo out the array, displays the following:// Array// (// [0] => comma// [1] => separated "values"// [2] => are// [3] => provided, here// [4] => for\nyou // )echo '<pre>';print_r($values);echo '</pre>';?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?