Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
How do I include equals and quotes when extracting from quotes in a string?

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);
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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] => ","
)

To get rid of the quotes then parse it as ini settings:

$result = parse_ini_string(implode("\n", $result[0]));

Yields:

Array
(
    [include_rows] => 1-10
    [debug_mode] => no
    [source_type] => guess
    [path] => largecsv
    [source_files] => test?output=csv
    [csv_delimiter] => ,
)
over 4 years ago · Santiago Trujillo Report

0

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] => ,
)
over 4 years ago · Santiago Trujillo Report

0

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] => ,
)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!