Lukas
Lukas

Reputation: 47

Compilation failed: POSIX collating elements are not supported at offset

i would like transform a string to array with pattern. But my regex give me the warning.

this is a string:

$string = typ="bar" title="Example" enabled=true count=true style="float: left; width: 30%;"

My regex:

$regex='/(.*?)[=\"|=](.*?)\"*\s*/';
preg_match_all($regex, $string1, $matchesreg, PREG_SET_ORDER);

Whith the regex is the output not correct. The last array must be split further $regex='/(.?)="(.?)"\s*/'; preg_match_all($regex, $string1, $matchesreg, PREG_SET_ORDER); The output

Array
     (  
         [0] => Array
             (
                 [0] => typ="bar" 
                 [1] => typ
                 [2] => bar
             ) ...
         [2] => Array
             (
                 [0] => enabled=true count=true style="float: left; width: 30%;"
                 [1] => enabled=true count=true style
                 [2] => float: left; width: 30%;
             )    
     )

My desired output like:

php
Array
(
    [0] => Array
        (
            [0] => typ="bar" 
            [1] => typ
            [2] => bar
        )    
    [1] => Array
        (
            [0] => title="Example" 
            [1] => title
            [2] => Example
        )    
    [2] => Array
        (
            [0] => enabled=true
            [1] => enabled
            [2] => true
        )    
    [3] => Array
        (
            [0] => count=true
            [1] => count
            [2] => true
        )    
    [4] => Array
        (
            [0] => style="float: left; width: 30%;"
            [1] => style
            [2] => float: left; width: 30%;
        )    
)

Upvotes: 1

Views: 254

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

You may use

preg_match_all('~([^\s=]+)=(?|"([^"]*)"|(\S+))~', $s, $m, PREG_SET_ORDER, 0)

See the PHP demo

Details

  • ([^\s=]+) - Group 1: one or more chars other than whitespace and =
  • = - a = char
  • (?|"([^"]*)"|(\S+)) - a branch reset group matching either of
    • "([^"]*)" - ", then any 0 or more chars other than " are captured into Group 2, and then " is matched
    • | - or
    • (\S+) - Group 2: one or more non-whitespace chars.

Upvotes: 2

Emma
Emma

Reputation: 27723

Your expression seems to be working fine. There were two = in your char list that I removed one of them:

(.*?)[=\"|](.*?)\"*\s* 

You can modify/change your expressions in this link, if you wish.

enter image description here

RegEx Circuit

You can visualize your expressions in this link:

enter image description here

Code

$string1 = 'typ="bar" title="Example" enabled=true count=true style="float: left; width: 30%;';
$regex = '/(.*?)[=\"|](.*?)\"*\s*/s';
preg_match_all($regex, $string1, $matchesreg, PREG_SET_ORDER);

var_dump($matchesreg);

Output

array(7) {
  [0]=>
  array(3) {
    [0]=>
    string(5) "typ=""
    [1]=>
    string(3) "typ"
    [2]=>
    string(0) ""
  }
  [1]=>
  array(3) {
    [0]=>
    string(5) "bar" "
    [1]=>
    string(3) "bar"
    [2]=>
    string(0) ""
  }
  [2]=>
  array(3) {
    [0]=>
    string(7) "title=""
    [1]=>
    string(5) "title"
    [2]=>
    string(0) ""
  }
  [3]=>
  array(3) {
    [0]=>
    string(9) "Example" "
    [1]=>
    string(7) "Example"
    [2]=>
    string(0) ""
  }
  [4]=>
  array(3) {
    [0]=>
    string(8) "enabled="
    [1]=>
    string(7) "enabled"
    [2]=>
    string(0) ""
  }
  [5]=>
  array(3) {
    [0]=>
    string(11) "true count="
    [1]=>
    string(10) "true count"
    [2]=>
    string(0) ""
  }
  [6]=>
  array(3) {
    [0]=>
    string(12) "true style=""
    [1]=>
    string(10) "true style"
    [2]=>
    string(0) ""
  }
}

Upvotes: -1

Related Questions