解析包含 3 个分隔符的字符串

发布于 2024-10-16 14:06:22 字数 1257 浏览 7 评论 0原文

我有一个二维数组,它是从字符串中分解出来的。一旦它爆炸,这就是输出:

---> 0 - 16~4~0.0~~~~false~~~~ 
---> 1 - 1000.0~21.75~L~1~2.0~2.0~L~2~ 
---> 2 - 
---> 0 - 2~5~951.3~6.4~~~false~~~~ 
---> 1 - 1000.0~11.77~L~1~ 
---> 2 - 
---> 0 - 3~6~1269.02~5.1~~~false~~~~ 
---> 1 - 5.0~213.66~L~1~4.9~2.56~L~2~4.6~19.5~L~3~ 
---> 2 - 5.1~53.44~B~1~5.4~8.48~B~2~5.5~15.53~B~3~ 

我想让它对于数组中的每个位置仅采用〜之前的第一个值。我不确定该怎么做。这是我到目前为止的代码:

$test = explode(":", $string);
foreach($test as &$value) $value = explode('|', $value);

以防万一您需要它,这是原始字符串输入:

1~1~828.32~12.5~~~假~~~~|1000.0~41.73~L~1~2.0~2.0~L~2~|:4~2~4.16~12.5~~~假~~ ~~|1000.0~21.75~L~1~2.0~2.0~L~2~|:9~3~0.16~24.0~~~假~~~~|1000.0~21.75~L~1~2.0~2.0~L ~2~|:16~4~0.0~~~~假~~~~|1000.0~21.75~L~1~2.0~2.0~L~2~|:2~5~951.3~6.4~~~假~ ~~~|1000.0~11.77~L~1~|:3~6~1269.02~5.1~~~假~~~~|5.0~213.66~L~1~4.9~2.56~L~2~4.6~19.5~ L~3~|5.1~53.44~B~1~5.4~8.48~B~2~5.5~15.53~B~3~:8~7~111.92~7.0~~~假~~~~|6.8~6.78~ L~1~6.6~148.39~L~2~6.4~3.7~L~3~|7.6~128.0'...

我希望输出为:

---> 0 - 16 
---> 1 - 1000.0
---> 2 - 
---> 0 - 2
---> 1 - 1000.0
---> 2 - 
---> 0 - 3
---> 1 - 5.0 
---> 2 - 5.1 

I have a 2D array which I have exploded from a string. Once it has exploded this is what is output:

---> 0 - 16~4~0.0~~~~false~~~~ 
---> 1 - 1000.0~21.75~L~1~2.0~2.0~L~2~ 
---> 2 - 
---> 0 - 2~5~951.3~6.4~~~false~~~~ 
---> 1 - 1000.0~11.77~L~1~ 
---> 2 - 
---> 0 - 3~6~1269.02~5.1~~~false~~~~ 
---> 1 - 5.0~213.66~L~1~4.9~2.56~L~2~4.6~19.5~L~3~ 
---> 2 - 5.1~53.44~B~1~5.4~8.48~B~2~5.5~15.53~B~3~ 

I want to make it so that for each position in the array it only takes the first value before the ~. I am unsure how to do this. This is the code I have so far:

$test = explode(":", $string);
foreach($test as &$value) $value = explode('|', $value);

Just in case you need it this is the original string input:

1~1~828.32~12.5~~~false~~~~|1000.0~41.73~L~1~2.0~2.0~L~2~|:4~2~4.16~12.5~~~false~~~~|1000.0~21.75~L~1~2.0~2.0~L~2~|:9~3~0.16~24.0~~~false~~~~|1000.0~21.75~L~1~2.0~2.0~L~2~|:16~4~0.0~~~~false~~~~|1000.0~21.75~L~1~2.0~2.0~L~2~|:2~5~951.3~6.4~~~false~~~~|1000.0~11.77~L~1~|:3~6~1269.02~5.1~~~false~~~~|5.0~213.66~L~1~4.9~2.56~L~2~4.6~19.5~L~3~|5.1~53.44~B~1~5.4~8.48~B~2~5.5~15.53~B~3~:8~7~111.92~7.0~~~false~~~~|6.8~6.78~L~1~6.6~148.39~L~2~6.4~3.7~L~3~|7.6~128.0'...

I would like the output to be:

---> 0 - 16 
---> 1 - 1000.0
---> 2 - 
---> 0 - 2
---> 1 - 1000.0
---> 2 - 
---> 0 - 3
---> 1 - 5.0 
---> 2 - 5.1 

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

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

发布评论

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

评论(4

依 靠 2024-10-23 14:06:22

如果我理解正确,您想要获取该数组的每个元素并修剪第一个 ~ 字符之后的所有内容。基于您的代码:

$test = explode(":", $string);
foreach($test as &$value)
{
    $value = explode('|', $value);
    foreach($value as &$inner_value)
    {
        $inner_value = substr($inner_value, 0, strpos($inner_value, '~'));
    }
}

我添加的只是一个内部 foreach 循环,它检查每个值并删除 ~ 字符之后的字符串的其余部分。

最好的编码!

If I understand correctly, you want to take each element of this array and trim off everything after the first ~ character. Building on your code:

$test = explode(":", $string);
foreach($test as &$value)
{
    $value = explode('|', $value);
    foreach($value as &$inner_value)
    {
        $inner_value = substr($inner_value, 0, strpos($inner_value, '~'));
    }
}

All I added was an inner foreach loop that inspects each value and removes the rest of the string after the ~ character.

Best of coding!

鱼窥荷 2024-10-23 14:06:22

如果我理解你的问题,这应该可行:

$output = array();
$test = explode(":", $string);
foreach($test as $value) {
   $pipes = explode('|',$value);
   foreach($pipes as $cur) {
       $idx = strpos($cur,'~');
       if($idx > -1) {
          $output[] = substr($cur,0,$idx);
       } else $output[] = '';
   }
}

执行完成后, $output 将包含所需的信息。您还可以编写递归函数或使用正则表达式,但这两种解决方案都会更复杂一些。

If I understand your question, this should work:

$output = array();
$test = explode(":", $string);
foreach($test as $value) {
   $pipes = explode('|',$value);
   foreach($pipes as $cur) {
       $idx = strpos($cur,'~');
       if($idx > -1) {
          $output[] = substr($cur,0,$idx);
       } else $output[] = '';
   }
}

After this is finished executing, $output will contain the desired information. You could also write a recursive function or use a regex, but both those solutions would be a bit more complex.

hth

各空 2024-10-23 14:06:22

如果您只想剪切字符串部分直到分隔符,请使用 strtok() 而不是explode()。

我不太明白你现在的例子。但它可能是:

foreach ($test as $i=>$value) { 
    $test[$i] = strtok($value, '|');
}

If you only want to cut out a string part until a delimiter, then use strtok() instead of explode().

I don't quite get your current example. But it might be:

foreach ($test as $i=>$value) { 
    $test[$i] = strtok($value, '|');
}
靑春怀旧 2024-10-23 14:06:22

您可以按 ~ 分解每一行并获取第一个元素。

$test = explode(":", $string);
foreach($test as &$value) {
    $value = explode('|', $value);
    $result = explode('~', $value);
    $first = $result[0];
}

但我认为如果您只需要第一个元素,这可能会变得非常低效。正则表达式可能是另一种解决方案。像这样:

\|(.+?)~

匹配 | 之后到第一个 ~ 的每个字符串。这会在字符串的开头处中断,但您可以在其前面添加 | 以进行正则表达式匹配。

You could explode each line by ~ and take the first element.

$test = explode(":", $string);
foreach($test as &$value) {
    $value = explode('|', $value);
    $result = explode('~', $value);
    $first = $result[0];
}

But I think this could become quite inefficient if you only need the first element. A regular expression could be another solution. Like this:

\|(.+?)~

Matching every string after a | up to the first ~. This breaks at the beginning of your string, but you could prepend that with a | for the regex matching.

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