将具有两个不同分隔符的字符串解析为关联数组

发布于 2024-12-14 23:39:49 字数 291 浏览 2 评论 0原文

我有一个如下所示的字符串变量。我试图将其拆分为一个数组,甚至将每个元素拆分为单独的变量(变化、日高等)。

$a='last: 901.5001 @ 11:35am EST 11/10/2011 <br>change: -5.9999 <br>day high: 
921.50 <br>day low: 882.00 <br>open: 917.50 <br>volume: 808998';
echo $a;

我有什么想法如何做到这一点?

I have a string variable like the one below. I'm trying to split it into an array or even into separate variables for each element (change, day high etc.).

$a='last: 901.5001 @ 11:35am EST 11/10/2011 <br>change: -5.9999 <br>day high: 
921.50 <br>day low: 882.00 <br>open: 917.50 <br>volume: 808998';
echo $a;

Any ideas how I do this?

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

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

发布评论

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

评论(4

奶气 2024-12-21 23:39:49

$tmps = explode('<br>', $string);
$arr = array();
foreach($tmps as $tmp){
    list($key, $val) = explode(':', $tmp,2);
    $arr[trim($key)] = trim($val);
}
var_dump($arr)

如果您不关心 之前的内容,您可以将 $arr[trim($key)] 行更改为 $arr[]

This should do

$tmps = explode('<br>', $string);
$arr = array();
foreach($tmps as $tmp){
    list($key, $val) = explode(':', $tmp,2);
    $arr[trim($key)] = trim($val);
}
var_dump($arr)

you can change the line $arr[trim($key)] to $arr[] if you do not care of what was before the :

从﹋此江山别 2024-12-21 23:39:49

您可以先在
上拆分,然后在第一个冒号上拆分:

$temp = explode("<br>",$a);
$data = array();
foreach ($temp as $item) {
   list($name, $value) = explode(":", $item, 2);
   $data[$name] = $value;
}

应该输出:

Array
(
   [last] => 901.5001 @ 11:35am EST 11/10/2011
   [change] => -5.9999
   [day high] => 921.50
   [open] => 882.00
   ... etc ...
)

You could split on <br> first, then split on the first colon:

$temp = explode("<br>",$a);
$data = array();
foreach ($temp as $item) {
   list($name, $value) = explode(":", $item, 2);
   $data[$name] = $value;
}

should output:

Array
(
   [last] => 901.5001 @ 11:35am EST 11/10/2011
   [change] => -5.9999
   [day high] => 921.50
   [open] => 882.00
   ... etc ...
)
乄_柒ぐ汐 2024-12-21 23:39:49
$step_one = explode('<br>', $string);
foreach($step_one as $step_two)
{
    $step_three = explode(': ', $step_two);
    $final[$step_three[0]] = $step_three[1];
}
var_dump($final);

或者获取您评论中的内容:

$step_one = explode('<br>', $string);
foreach($step_one as $step_two)
{
    $step_three = explode(': ', $step_two);
    $final[] = $step_three[1];
}
var_dump($final);
$step_one = explode('<br>', $string);
foreach($step_one as $step_two)
{
    $step_three = explode(': ', $step_two);
    $final[$step_three[0]] = $step_three[1];
}
var_dump($final);

Or to get what's in your comment:

$step_one = explode('<br>', $string);
foreach($step_one as $step_two)
{
    $step_three = explode(': ', $step_two);
    $final[] = $step_three[1];
}
var_dump($final);
大海や 2024-12-21 23:39:49

在 HTML 上使用字符串匹配(尤其是正则表达式)可能很危险,因为任何细微的变化都可能会造成混乱。但是,如果您的代码被认为是好的:

preg_match_all('/([^:]+):(.+)<br>/U', $a.'<br>', $m, PREG_SET_ORDER);
$a = array();
foreach ($m as $v) $a[$v[1]] = trim($v[2]);

var_dump($a);

如果您不需要密钥,那么它就会变成:

preg_match_all('/:(.+)<br>/U', $a.'<br>', $m, PREG_SET_ORDER);
$a = array();
foreach ($m as $v) $a[] = trim($v[1]);

甚至:

preg_match_all('/:\s(.+)\s<br>/U', $a.'<br>', $m);
$a = $m[1];

Using string matching (especially regular expressions) on HTML can be dangerous as any slight variation may mess things up. But if your code is known to be good:

preg_match_all('/([^:]+):(.+)<br>/U', $a.'<br>', $m, PREG_SET_ORDER);
$a = array();
foreach ($m as $v) $a[$v[1]] = trim($v[2]);

var_dump($a);

If you don't want the keys then it just becomes:

preg_match_all('/:(.+)<br>/U', $a.'<br>', $m, PREG_SET_ORDER);
$a = array();
foreach ($m as $v) $a[] = trim($v[1]);

Or even:

preg_match_all('/:\s(.+)\s<br>/U', $a.'<br>', $m);
$a = $m[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文