0

I need a preg function which do the following job for following string-

string = {my_special_shortcode_name var1='some_var1' var2='some_var2' var3='some_var3'}

and the extracted string must look like-

Array( 'var1' =>  'some_var1',
       'var2' => 'some_var2',
       'var3' => 'some_var3'
)

Obviously I need the some pref function, but I have tried separating the string based on space formatting, but when I include spaces in any parameter of var1/var2/var3, for ex-

When I given this input instead-

string = {my_special_shortcode_name var1='some var1' var2='some_var2' var3='some_var3'}

NOTE THE SPACE IN SOME VAR1 (INSTEAD OF SOME_VAR1)

The following output is obtained-

Array( 'var1' =>  'some',
       'var1' => 'var2',
       'some_var2' => 'var3'
)

So I need a function which separate the string based on space outside the "" , not the space within "" .

Any idea how?

EDIT: I have successfully extracted the string into array. Problem lies in URL containing '?' which makes the separate string within "". So now, I need someone to suggest how to escape '?' and separate string into array?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
ashutosh
  • 1,192
  • 5
  • 22
  • 46

4 Answers4

0

You could always split it off of /[^\\]' /. So this will get '_, but not \'_ (where _ is your space) so that you can have an escape character inside your values for apostrophes.

Wolfman Joe
  • 799
  • 1
  • 8
  • 23
  • following warning issued- `Compilation failed: missing terminating ] for character class at offset 5` – ashutosh Mar 15 '13 at 18:11
  • Hrm. Sounds like the escape of the escape slash isn't working. It thinks that it's seeing `[^\]` instead of `[^\\]`. Are you passing it through anything that might collapse the escape slashes? I can't see your code so I can't really say why, exactly, it's getting that error message. – Wolfman Joe Mar 15 '13 at 18:25
  • Oh! I see you are successfully extracting the string. Might I suggest that for new questions, you ask a new question, instead of editing the old one? – Wolfman Joe Mar 15 '13 at 18:26
0

Youc can you regex like /var[\d]+='([\w ]+)'/

$pattern = "/var[\d]+='([\w ]+)'/";
$subject = "{my_special_shortcode_name var1='some var1' var2='some_var2' var3='some_var3'}";
preg_match_all( $pattern, $subject, $matches);

$matches will be filled with:

array (
  0 => 
  array (
    0 => 'var1=\'some var1\'',
    1 => 'var2=\'some_var2\'',
    2 => 'var3=\'some_var3\'',
  ),
  1 => 
  array (
    0 => 'some var1',
    1 => 'some_var2',
    2 => 'some_var3',
  ),
)
bstrx
  • 71
  • 2
0

I'd use preg_match_all function like:

$str = "{my_special_shortcode_name var1='some var1' var2='some_var2' var3='some_var3'}";
preg_match_all("~(\w+)\s*=\s*'([^']+)'~", $str, $m);
print_r($m);

output:

Array
(
    [0] => Array
        (
            [0] => var1='some var1'
            [1] => var2='some_var2'
            [2] => var3='some_var3'
        )

    [1] => Array
        (
            [0] => var1
            [1] => var2
            [2] => var3
        )

    [2] => Array
        (
            [0] => some var1
            [1] => some_var2
            [2] => some_var3
        )

)
Toto
  • 89,455
  • 62
  • 89
  • 125
0
$string = "{my_special_shortcode_name var1='http://stackoverflow.com/q/15438976/1020526?test' var2='some_var2' var3='some_var3'}";
preg_match_all("/\b(\w+)='([^']+)'/", $string, $matches);
$arr = array_combine($matches[1], $matches[2]); // first captured groups as key, and the second as the array values
print_r($arr);

Output:

Array
(
    [var1] => http://stackoverflow.com/q/15438976/1020526?test
    [var2] => some_var2
    [var3] => some_var3
)
revo
  • 47,783
  • 14
  • 74
  • 117