I have this string:
$shortcode = '[csvtohtml_create include_rows="1-10"
debug_mode="no" source_type="guess" path="largecsv"
source_files="test?output=csv" csv_delimiter="," ]'
I want to retrieve attributes and their values even their equal-signs or spaces within quotes.
This question is based on this question: How to explode a string but not within quotes in php?
I have this code:
$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 );
I get this result: (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" => ","
The result I want is:
Array attrs
"include_rows" => "1-10"
"debug_mode" => "no"
"source_type" => "guess"
"path" => "largecsv"
"source_files" => "test.csv?output=csv"
"csv_delimiter" => ","
etc...
I guess I should put in equal-sign (or is it ?=) somewhere in here but regex is not my strength...
$args = preg_split('/"[^"]+"(*SKIP)(*F)|\h+/', $shortcode);