解析文本并从每行两个子字符串填充关联数组

发布于 2024-12-11 09:00:28 字数 330 浏览 0 评论 0原文

给定一大串文本,我想搜索以下模式:

@key: value

所以一个例子是:

some crazy text
more nonesense
@first: first-value;
yet even more non-sense
@second: second-value;
finally more non-sense

输出应该是:

array("first" => "first-value", "second" => "second-value");

Given a large string of text, I want to search for the following patterns:

@key: value

So an example is:

some crazy text
more nonesense
@first: first-value;
yet even more non-sense
@second: second-value;
finally more non-sense

The output should be:

array("first" => "first-value", "second" => "second-value");

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

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

发布评论

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

评论(5

醉殇 2024-12-18 09:00:28
<?php


$string = 'some crazy text
more nonesense
@first: first-value;
yet even more non-sense
@second: second-value;
finally more non-sense';

preg_match_all('#@(.*?): (.*?);#is', $string, $matches);

$count = count($matches[0]);

for($i = 0; $i < $count; $i++)
{
    $return[$matches[1][$i]] = $matches[2][$i];
}

print_r($return);

?>

链接http://ideone.com/fki3U

数组(
[第一] =>第一值
[第二] =>第二个值)

<?php


$string = 'some crazy text
more nonesense
@first: first-value;
yet even more non-sense
@second: second-value;
finally more non-sense';

preg_match_all('#@(.*?): (.*?);#is', $string, $matches);

$count = count($matches[0]);

for($i = 0; $i < $count; $i++)
{
    $return[$matches[1][$i]] = $matches[2][$i];
}

print_r($return);

?>

Link http://ideone.com/fki3U

Array (
[first] => first-value
[second] => second-value )

姐不稀罕 2024-12-18 09:00:28

在 PHP 5.3 中测试:

    // set-up test string and final array
    $myString = "@test1: test1;@test2: test2;";
    $myArr = array();

    // do the matching
    preg_match_all('/@([^\:]+)\:([^;]+);/', $myString, $matches);

    // put elements of $matches in array here
    $actualMatches = count($matches) - 1;
    for ($i=0; $i<$actualMatches; $i++) {
        $myArr[$matches[1][$i]] = $matches[2][$i];
    }
    print_r($myArr);

背后的原因是:

  1. 正则表达式创建两个捕获组。一个捕获组是关键,
    该密钥的其他数据。捕获组是正则表达式的部分
    在左右香蕉里面,即(...)。
  2. $actualMatches 只是根据 preg_match_all 返回一个事实进行调整
    包含所有匹配项的额外元素。

演示

Tested in PHP 5.3:

    // set-up test string and final array
    $myString = "@test1: test1;@test2: test2;";
    $myArr = array();

    // do the matching
    preg_match_all('/@([^\:]+)\:([^;]+);/', $myString, $matches);

    // put elements of $matches in array here
    $actualMatches = count($matches) - 1;
    for ($i=0; $i<$actualMatches; $i++) {
        $myArr[$matches[1][$i]] = $matches[2][$i];
    }
    print_r($myArr);

The reasoning behind this is this:

  1. The regex is creating two capture groups. One capture group is the key, the
    other the data for that key. The capture groups are the portions of the regex
    inside left and right bananas, i.e., (...).
  2. $actualMatches just adjusts for the fact that preg_match_all returns an
    extra element containing all matches lumped together.

Demo.

猫瑾少女 2024-12-18 09:00:28

匹配以 @ 开头并以 ; 结尾的整个限定行。

捕获不包含任何冒号的子字符串作为第一组,并捕获冒号后面的空格与行尾分号之间的子字符串。

通过使用第二个捕获组中的任意字符点,子字符串可以包含分号,而不会损坏任何提取的数据。

调用 array_combine() 来形成两个捕获组之间的键值关系。

代码:(演示

preg_match_all(
    '/^@([^:]+): (.+);$/m',
    $text,
    $m
);
var_export(array_combine($m[1], $m[2]));

输出:

array (
  'first' => 'first-value',
  'second' => 'second-value',
)

Match whole qualifying lines starting with @ and ending with ;.

Capture the substring that does not contain any colons as the first group and capture the substring between the space after the colon and the semicolon at the end of the line.

By using the any character dot in the second capture group, the substring may contain a semicolon without damaging any extracted data.

Call array_combine() to form key-value relationships between the two capture groups.

Code: (Demo)

preg_match_all(
    '/^@([^:]+): (.+);$/m',
    $text,
    $m
);
var_export(array_combine($m[1], $m[2]));

Output:

array (
  'first' => 'first-value',
  'second' => 'second-value',
)
鸩远一方 2024-12-18 09:00:28

您可以尝试逐行循环字符串(分解和 foreach),并检查该行是否以 @(子字符串)开头,如果有,则通过 : 分解该行。

http://php.net/manual/en/function.explode.php

http://nl.php.net/manual/en/control-structs .foreach.php

http://nl.php.net/manual/en/function.substr.php

You can try looping the string line by line (explode and foreach) and check if the line starts with an @ (substr) if it has, explode the line by :.

http://php.net/manual/en/function.explode.php

http://nl.php.net/manual/en/control-structures.foreach.php

http://nl.php.net/manual/en/function.substr.php

寻找一个思念的角度 2024-12-18 09:00:28

根据您的输入字符串的样子,您可能可以简单地使用 parse_ini_string,或者对字符串进行一些小的更改,然后使用该函数。

Depending on what your input string looks like, you might be able to simply use parse_ini_string, or make some small changes to the string then use the function.

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