0

Please, help solve the problem. I have a string like this:

[MoveNode(node='/html/head/meta[6]', target='/html/head[1]', position=22), MoveNode(node='/html/body/div[1]/main/div[1]/div/div/div/div/div[2]/ul/li[1]/a[1]', target='/html/head[1]', position=15), RenameNode(node='/html/head/a[1]', tag='meta'), InsertAttrib(node='/html/head/meta[6]', name='content', value='text text text text (text text), text'), InsertAttrib(node='/html/head/meta[6]', name='name', value='description'), DeleteAttrib(node='/html/head/meta[6]', name='href'), DeleteAttrib(node='/html/head/meta[6]', name='title'), UpdateTextIn(node='/html/head/meta[6]', text=None), MoveNode(node='/html/body/div[1]/main/footer/ul/li[2]/a[1]', target='/html/head[1]', position=16), RenameNode(node='/html/head/a[1]', tag='meta'), DeleteNode(node='/html/body/div[1]/main/div[1]/div/header/div/div/div[2]/div/h6[1]'), DeleteNode(node='/html/body/div[1]/main/div[1]/div/header/div/div/div[2]/div[1]')]

I need to convert this string to such an array. How to do it?

Array
(
    [MoveNode_1] => Array
        (
            [node] => /html/head/meta[6]
            [target] => /html/head[1]
            [position] => 22
        )
...
    [InsertAttrib_4] => Array
        (
            [node] => /html/head/meta[6]
            [name] => content
            [value] => text text text text (text text), text
        )

    [InsertAttrib_5] => Array
        (
            [node] => /html/head/meta[6]
            [name] => name
            [value] => description
        )
...

    [MoveNode_9] => Array
        (
            [node] => /html/body/div[1]/main/footer/ul/li[2]/a[1]
            [target] => /html/head[1]
            [position] => 16
        )
...
)

Tried to solve like this

$str = '....'; 
$arr0 = explode("), ", $str);
$arr2 = [];
$i = 1;
foreach ($arr0 AS $arr1) {
    $arrs = explode("(", $arr1);
    parse_str(str_replace(['\'', ', '], ['', '&'], $arrs[1]), $output);
    $arr2[$arrs[0] . "_" . $i] = $output;
    $i++;
}
print_r($arr2);
But because of the brackets inside the text, the array is formed incorrectly
4
  • 1
    This looks like a print_r/var_dumped object. Where did you get this from? Should've generated a parsable format to begin with, instead of this debug output. Commented Dec 20, 2022 at 4:32
  • This generates the code from the example. But he does it wrong. I showed how to get the ideal Commented Dec 20, 2022 at 4:37
  • "But because of the brackets inside the text, the array is formed incorrectly"; incorrectly how? please be more specific. Commented Dec 20, 2022 at 4:48
  • like this: ``` [InsertAttrib_4] => Array ( [node] => /html/head/meta[6] [name] => content [value] => text text text text ) [text'_5] => Array ( ) [InsertAttrib_6] => Array ( [node] => /html/head/meta[6] [name] => name [value] => description ) ``` Commented Dec 20, 2022 at 4:49

1 Answer 1

1

You can use a combination of preg_match_all and array_map to extract the function names, node strings, and key-value pairs from the string.

 $text = '[MoveNode(node=\'/html/head/meta[6]\', target=\'/html/head[1]\', position=22), MoveNode(node=\'/html/body/div[1]/main/div[1]/div/div/div/div/div[2]/ul/li[1]/a[1]\', target=\'/html/head[1]\', position=15), RenameNode(node=\'/html/head/a[1]\', tag=\'meta\'), InsertAttrib(node=\'/html/head/meta[6]\', name=\'content\', value=\'text text text text (text text), text\'), InsertAttrib(node=\'/html/head/meta[6]\', name=\'name\', value=\'description\'), DeleteAttrib(node=\'/html/head/meta[6]\', name=\'href\'), DeleteAttrib(node=\'/html/head/meta[6]\', name=\'title\'), UpdateTextIn(node=\'/html/head/meta[6]\', text=None), MoveNode(node=\'/html/body/div[1]/main/footer/ul/li[2]/a[1]\', target=\'/html/head[1]\', position=16), RenameNode(node=\'/html/head/a[1]\', tag=\'meta\'), DeleteNode(node=\'/html/body/div[1]/main/div[1]/div/header/div/div/div[2]/div/h6[1]\'), DeleteNode(node=\'/html/body/div[1]/main/div[1]/div/header/div/div/div[2]/div[1]\')]';

   $str = preg_replace_callback("/(?<!\\\)'.*?(?<!\\\)'/s", function ($m) {
    return str_replace(['(', ')', ','], ['&lpar;‎', '&rpar;‏', '&sbquo;'], $m[0]);
}, $text);

     preg_match_all('/([\w]+)\(([^)]+)\)/', $str, $matches);
            $functions = $matches[1];
            $nodes = $matches[2];
            
            $result = array_map(function ($function, $node) {
                preg_match_all('/(\w+)=([^,]+)/', $node, $node_matches);
                $keys = $node_matches[1];
                $values = $node_matches[2];
                return [$function => array_combine($keys, $values)];
            }, $functions, $nodes);
            echo "<pre>";
            print_r($result);
            exit;

the resulted array be like

Array
    (
        [0] => Array
            (
                [MoveNode] => Array
                    (
                        [node] => '/html/head/meta[6]'
                        [target] => '/html/head[1]'
                        [position] => 22
                    )
    
            )
    
        [1] => Array
   

     (
            [MoveNode] => Array
                (
                    [node] => '/html/body/div[1]/main/div[1]/div/div/div/div/div[2]/ul/li[1]/a[1]'
                    [target] => '/html/head[1]'
                    [position] => 15
                )

        )

    [2] => Array
        (
            [RenameNode] => Array
                (
                    [node] => '/html/head/a[1]'
                    [tag] => 'meta'
                )

        )

    [3] => Array
        (
            [InsertAttrib] => Array
                (
                    [node] => '/html/head/meta[6]'
                    [name] => 'content'
                    [value] => 'text text text text (text text), text'
                )

        )

    [4] => Array
        (
            [InsertAttrib] => Array
                (
                    [node] => '/html/head/meta[6]'
                    [name] => 'name'
                    [value] => 'description'
                )

        )

    [5] => Array
        (
            [DeleteAttrib] => Array
                (
                    [node] => '/html/head/meta[6]'
                    [name] => 'href'
                )

        )

    [6] => Array
        (
            [DeleteAttrib] => Array
                (
                    [node] => '/html/head/meta[6]'
                    [name] => 'title'
                )

        )

    [7] => Array
        (
            [UpdateTextIn] => Array
                (
                    [node] => '/html/head/meta[6]'
                    [text] => None
                )

        )

    [8] => Array
        (
            [MoveNode] => Array
                (
                    [node] => '/html/body/div[1]/main/footer/ul/li[2]/a[1]'
                    [target] => '/html/head[1]'
                    [position] => 16
                )

        )

    [9] => Array
        (
            [RenameNode] => Array
                (
                    [node] => '/html/head/a[1]'
                    [tag] => 'meta'
                )

        )

    [10] => Array
        (
            [DeleteNode] => Array
                (
                    [node] => '/html/body/div[1]/main/div[1]/div/header/div/div/div[2]/div/h6[1]'
                )

        )

    [11] => Array
        (
            [DeleteNode] => Array
                (
                    [node] => '/html/body/div[1]/main/div[1]/div/header/div/div/div[2]/div[1]'
                )

        )

)
Sign up to request clarification or add additional context in comments.

2 Comments

Pretty much what I need, thanks! Only there is a problem. The text after ")" is removed. how to avoid it?
Found a solution. First, this text needs to be processed with this function: $str = preg_replace_callback("/(?<!\\)'.*?(?<!\\)'/s", function ($m) { return str_replace(['(', ')', ','], ['&lpar;‎', '&rpar;‏', '&sbquo;'], $m[0]); }, $text);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.