将每行具有两个分隔符的多行字符串解析为关联行数组

发布于 2025-01-07 08:13:41 字数 449 浏览 0 评论 0原文

我发现了一些将数据分解为数组的帖子,但我的有点具体,因为数据有 2 个部分

,我有这个

title=Title1|link=Link1
title=Title2|link=Link2

,结果我需要的是这个

Array
(
    [0] => Array
        (
            [title] => Title1
            [link] => Link1
        )

    [1] => Array
        (
            [title] => Title2
            [link] => Link2
            
        )

)

数据来自 texarea,用 \n 分隔,所以你看到的数据是实际数据。

I found a few posts to explode data as array, but mine is bit specific because the data has 2 parts

I have this

title=Title1|link=Link1
title=Title2|link=Link2

and result I need is this

Array
(
    [0] => Array
        (
            [title] => Title1
            [link] => Link1
        )

    [1] => Array
        (
            [title] => Title2
            [link] => Link2
            
        )

)

data is coming from texarea separated by \n so the data you see is actual data.

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

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

发布评论

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

评论(3

乱世争霸 2025-01-14 08:13:41
preg_match_all('~^title=(.+)\|link=(.+)$~m', $text, $reg, PREG_SET_ORDER);

print_r($reg) 生成

Array
(
    [0] => Array
        (
            [0] => title=Title1|link=Link1
            [1] => Title1
            [2] => Link1
        )

    [1] => Array
        (
            [0] => title=Title2|link=Link2
            [1] => Title2
            [2] => Link2
        )

)

我相信您可以轻松修改它以适合您所需的架构。

preg_match_all('~^title=(.+)\|link=(.+)$~m', $text, $reg, PREG_SET_ORDER);

print_r($reg) produces

Array
(
    [0] => Array
        (
            [0] => title=Title1|link=Link1
            [1] => Title1
            [2] => Link1
        )

    [1] => Array
        (
            [0] => title=Title2|link=Link2
            [1] => Title2
            [2] => Link2
        )

)

I'm sure you can easily modify that to fit your required schema.

宁愿没拥抱 2025-01-14 08:13:41

当您实际上不需要正则表达式时,我更喜欢 explode() 而不是 preg_match()

<?php

$text="title=Title1|link=Link1\ntitle=Title2|link=Link2";

$result=array();
$count=0;

// line by line...
foreach (explode("\n", $text) as $line) {
  // variable by variable...
  foreach (explode("|", $line) as $vars) {
    // separate LHS from RHS.
    $parts=explode("=", $vars);
    $result[$count][$parts[0]]=$parts[1];
  }
  $count++;
}

print_r($result);

?>

不要忘记添加代码来处理与您期望的模式不匹配的行。输入验证很重要。

I'm more a fan of explode() than preg_match() when you don't actually need regular expressions.

<?php

$text="title=Title1|link=Link1\ntitle=Title2|link=Link2";

$result=array();
$count=0;

// line by line...
foreach (explode("\n", $text) as $line) {
  // variable by variable...
  foreach (explode("|", $line) as $vars) {
    // separate LHS from RHS.
    $parts=explode("=", $vars);
    $result[$count][$parts[0]]=$parts[1];
  }
  $count++;
}

print_r($result);

?>

Don't forget to add code to handle lines that don't match the pattern you expect. Input validation is important.

情感失落者 2025-01-14 08:13:41

也许像这样,它有点冗长,但您应该能够使用它作为更优雅的东西的基础。

<?
$result=array();

foreach ($line in $iterable_variable_with_one_line_per_iteration){
    $subresult=array();
    foreach ($kv_pair in explode($line,"|")){
          $kv_pair_2 = explode($kv_pair,"=");
          $subresult[kv_pair_2[0]]=$kv_pair_2[1];
    }
    array_push($result,$subresult);
}
?>

Maybe something like this, it's a bit verbose but you should be able to use it as the basis for something more elegant.

<?
$result=array();

foreach ($line in $iterable_variable_with_one_line_per_iteration){
    $subresult=array();
    foreach ($kv_pair in explode($line,"|")){
          $kv_pair_2 = explode($kv_pair,"=");
          $subresult[kv_pair_2[0]]=$kv_pair_2[1];
    }
    array_push($result,$subresult);
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文