Home > Mobile >  How do I include equals and quotes when extracting from quotes in a string?
How do I include equals and quotes when extracting from quotes in a string?

Time:10-23

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);

CodePudding user response:

You were on the right track with exploding the equals sign. This implementation does array_shift to get the key, then implodes the rest of the array together.

<?php

$sc = '[csvtohtml_create include_rows="1-10" 
debug_mode="no" source_type="guess" path="largecsv" 
source_files="test?output=csv"  csv_delimiter="," ]';

$args = array_filter(array_map('trim', preg_split('/"[^"] "(*SKIP)(*F)|\h /', $sc)));
array_shift($args); // remove [csvtohtml
array_pop($args); // remove ]

$output = [];
foreach ($args as $arg) {
    $explode = explode('=', $arg);
    $key = array_shift($explode);
    $value = str_replace('"', '', implode('=', $explode)); // remove str_replace if you want to keep the extra set of quotes
    $output[$key] = $value;
}

print_r($output);

Results in

Array
(
    [include_rows] => 1-10
    [debug_mode] => no
    [source_type] => guess
    [path] => largecsv
    [source_files] => test?output=csv
    [csv_delimiter] => ,
)

CodePudding user response:

Might be easier to get the matches. Then you can do whatever. I turn it into a query string and parse into an array:

preg_match_all('/[^\s=] ="[^"]*"/', $shortcode, $result);
parse_str(implode('&', $result[0]), $result);

Now $result yields:

Array
(
    [include_rows] => "1-10"
    [debug_mode] => "no"
    [source_type] => "guess"
    [path] => "largecsv"
    [source_files] => "test?output=csv"
    [csv_delimiter] => ","
)

CodePudding user response:

You can match the attribute names and their values using

([^\s=] )=(?|"([^"]*)"|(\S ))

See the regex demo. Details:

  • ([^\s=] ) - Group 1: one or more chars other than whitespace and = chars
  • = - an = sign
  • (?|"([^"]*)"|(\S )) - Branch reset group:
    • "([^"]*)" - ", then any zero or more chars other than " captured into Group 1, and then a "
    • | - or
    • (\S ) - Group 2: any one or more non-whitespace chars

See the PHP demo:

$sc = '[csvtohtml_create include_rows="1-10" 
debug_mode="no" source_type="guess" path="largecsv" 
source_files="test?output=csv"  csv_delimiter="," ]';
if (preg_match_all('~([^\s=] )=(?|"([^"]*)"|(\S ))~', $sc, $ms)) {
    print_r(array_combine($ms[1],$ms[2]));
}

Output:

Array
(
    [include_rows] => 1-10
    [debug_mode] => no
    [source_type] => guess
    [path] => largecsv
    [source_files] => test?output=csv
    [csv_delimiter] => ,
)
  • Related