php 如何将字符串转换为数组?

5kgi1eie  于 2022-12-21  发布在  PHP
关注(0)|答案(1)|浏览(137)

请帮助解决这个问题。我有这样一个字符串:
[移动节点(节点=“/html/head/ meta[6]”,目标=“/html/head[1]”,位置=22),移动节点(节点=“/html/主体/div[1]/主要/div[1]/div/div/div/div[2]/ul/li[1]/a[1]”,目标=“/html/head[1]”,位置=15),重命名节点(节点=“/html/head/a[1]”,标记=“元数据”),插入属性(节点=“/html/head/ meta[6]”,名称=“内容”,值=“文本文本文本文本(文本文本),文本”),插入属性(节点=“/html/head/ meta[6]”,名称=“名称”,值=“说明”),删除属性(节点=“/html/head/ meta[6]”,名称=“href”),删除属性(节点=“/html/head/ meta[6]”,名称=“标题”),更新文本输入(节点=“/html/head/ meta[6]”,文本=无),移动节点(节点=“/html/主体/div[1]/主体/页脚/ul/li[2]/a[1]”,目标=“/html/head[1]”,位置=16),重命名节点(节点=“/html/head/a[1]”,标记=“meta”),删除节点(节点=“/html/body/div[1]/主要/div[1]/div/标题/div/div/div[2]/div/h6[1]”),删除节点(节点=“/html/body/div[1]/主要/div[1]/div/标题/div/div/div[2]/div[1]”)]
我需要把这个字符串转换成这样一个数组.怎么做?

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

试图像这样解决
x一个一个一个一个x一个一个二个x

qlfbtfca

qlfbtfca1#

可以组合使用preg_match_allarray_map从字符串中提取函数名、节点字符串和键值对。

$str = '[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]\')]';



 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;

所得到阵列类似于

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

        )

    [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]'
                )

        )

)

相关问题