php 大括号放入数组

发布于 2024-08-28 17:34:11 字数 354 浏览 6 评论 0原文

我想检查打开的 .txt 文件中是否有像下面这样打开和关闭的大括号:

file {

nextopen {
//content
}

}

不,这不是我自己的语言或任何东西,但我想知道 nextopen 函数和括号内的所有内容,以及所有内容如果您知道我的意思,请从文件函数内部将其添加到数组中。所以大括号内的所有内容都将在一个数组中。如果您知道如何执行此操作,请回复。

数组应该如下所示:

array(
[file] => '{ nextopen { //content } }',
[nextopen] => '{ //content }'
);

I would like to check a opened .txt file for braces that open and close like below:

file {

nextopen {
//content
}

}

no this is not my own language or anything, but I want to get say the nextopen function and all the contents inside the brackets, and all the stuff from inside the file function and add it to an array if you know what i mean. so all the content inside the braces will be in an array. if you know how to do this please reply.

array should look like this:

array(
[file] => '{ nextopen { //content } }',
[nextopen] => '{ //content }'
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-09-04 17:34:11

其基本算法如下,

  1. 对于每个序列 { no-braces-here },将其放入缓冲区中,并用一个标识其在缓冲区中位置的幻数替换,
  2. 重复 (1) 直到没有可以
  3. 为缓冲区中的每个条目找到更多序列 - 如果它包含幻数,则将每个数字替换为缓冲区中相应的字符串。
  4. 缓冲区是我们

在 php

class Parser
{
    var $buf = array();

    function put_to_buf($x) {
        $this->buf[] = $x[0];
        return '@' . (count($this->buf) - 1) . '@';
    }

    function get_from_buf($x) {
        return $this->buf[intval($x[1])];
    }

    function replace_all($re, $str, $callback) {
        while(preg_match($re, $str))
            $str = preg_replace_callback($re, array($this, $callback), $str);
        return $str;
    }

    function run($text) {
        $this->replace_all('~{[^{}]*}~', $text, 'put_to_buf');
        foreach($this->buf as &$s)
            $s = $this->replace_all('~@(\d+)@~', $s, 'get_from_buf');
        return $this->buf;
    }



}

测试

$p = new Parser;
$a = $p->run("just text { foo and { bar and { baz } and { quux } } hello! } ??");
print_r($a);

结果

Array
(
    [0] => { baz }
    [1] => { quux }
    [2] => { bar and { baz } and { quux } }
    [3] => { foo and { bar and { baz } and { quux } } hello! }
)

中寻找的内容,如果您有任何疑问,请告诉我。

The basic algo for this is like the following

  1. for every sequence { no-braces-here }, put it in a buffer and replace with a magic number identifying its position in the buffer
  2. repeat (1) until no more sequences can be found
  3. for every entry in a buffer - if it contains magic numbers, replace each number with the corresponding string from the buffer.
  4. the buffer is what we're looking for

in php

class Parser
{
    var $buf = array();

    function put_to_buf($x) {
        $this->buf[] = $x[0];
        return '@' . (count($this->buf) - 1) . '@';
    }

    function get_from_buf($x) {
        return $this->buf[intval($x[1])];
    }

    function replace_all($re, $str, $callback) {
        while(preg_match($re, $str))
            $str = preg_replace_callback($re, array($this, $callback), $str);
        return $str;
    }

    function run($text) {
        $this->replace_all('~{[^{}]*}~', $text, 'put_to_buf');
        foreach($this->buf as &$s)
            $s = $this->replace_all('~@(\d+)@~', $s, 'get_from_buf');
        return $this->buf;
    }



}

test

$p = new Parser;
$a = $p->run("just text { foo and { bar and { baz } and { quux } } hello! } ??");
print_r($a);

result

Array
(
    [0] => { baz }
    [1] => { quux }
    [2] => { bar and { baz } and { quux } }
    [3] => { foo and { bar and { baz } and { quux } } hello! }
)

let me know if you have any questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文