【发布时间】:2021-12-09 11:38:21
【问题描述】:
我有这个字符串:
$shortcode = '[csvtohtml_create include_rows="1-10"
debug_mode="no" source_type="guess" path="largecsv"
source_files="test?output=csv" csv_delimiter="," ]'
我想检索属性及其值,甚至是引号内的等号或空格。
这个问题是基于这个问题:How to explode a string but not within quotes in php?
我有这个代码:
$args = preg_split('/"[^"]+"(*SKIP)(*F)|\h+/', $shortcode);
$attrs = [];
foreach( $args as $item )
{
//Only use those items in array $args with = sign
if ( strpos( $item , '=' ) !== false )
{
$sep = explode( '=', $item );
$key = $sep[0];
$value = $sep[1];
$attrs[$key] = str_replace( '"', '', $value );
}
}
$args = explode( '=', $sc );
我得到这个结果:(source_files without output=csv)
Array attrs
"include_rows" => "1-10"
"debug_mode" => "no"
"source_type" => "guess"
"path" => "largecsv"
"source_files" => "test.csv?output"
"csv_delimiter" => ","
我想要的结果是:
Array attrs
"include_rows" => "1-10"
"debug_mode" => "no"
"source_type" => "guess"
"path" => "largecsv"
"source_files" => "test.csv?output=csv"
"csv_delimiter" => ","
等等……
我想我应该在这里的某个地方输入等号(或者是?=),但正则表达式不是我的强项......
$args = preg_split('/"[^"]+"(*SKIP)(*F)|\h+/', $shortcode);
【问题讨论】: