首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将以逗号分隔的字符串拆分为数组,然后再用冒号分隔该数组?

如何将以逗号分隔的字符串拆分为数组,然后再用冒号分隔该数组?
EN

Stack Overflow用户
提问于 2015-09-20 12:24:46
回答 4查看 167关注 0票数 2

我有一个字符串,它是逗号分隔的。我用used将它用逗号分隔开,这给了我数组的结果。在那个数组中,我有需要用:分隔的数据。我怎样才能做到这一点?下面是字符串和输出。

弦乐:

代码语言:javascript
复制
$ing= 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon'
$output = explode(',', $ing);

输出:

代码语言:javascript
复制
Array ( [0] => coconut water :3-4 cups 
    [1] => coconut flesh : ½ cup 
    [2] => tea :4 tablespoon 
    [3] => Ice cubes :24-32 
    [4] => Lemon juice :4 tablespoon 
    [5] => Honey :8 tablespoon )

现在,我要单独的椰子水:3-4杯的结肠。

代码语言:javascript
复制
$ing= 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon'
$output1 = explode(',', $ing);
$output=array();
foreach($output1 as $item){
  $output[]=explode(':', $item);
}

输出:

代码语言:javascript
复制
Array
(
[0] => Array
    (
        [0] => coconut water 
        [1] => 3-4 cups
    )

[1] => Array
    (
        [0] => 
coconut flesh 
        [1] =>  ½ cup
    )

[2] => Array
    (
        [0] => 
tea 
        [1] => 4 tablespoon
    )

[3] => Array
    (
        [0] => 
Ice cubes 
        [1] => 24-32
    )

[4] => Array
    (
        [0] => 
Lemon juice 
        [1] => 4 tablespoon
    )

[5] => Array
    (
        [0] => 
 Honey 
        [1] => 8 tablespoon
    )

)

期望产出:

代码语言:javascript
复制
<tr>
  <td>coconut water </td>
  <td>3-4 cups</td>
</tr>
<tr>
  <td>coconut flesh</td>
  <td>½ cup</td>
</tr>
<tr>
  <td>tea </td>
  <td>4 tablespoon</td>
</tr>
<tr>
  <td>Ice cubes</td>
  <td>24-32</td>
</tr>
<tr>
  <td>Lemon juice</td>
  <td>4 tablespoon</td>
</tr>
<tr>
  <td>Honey</td>
  <td>8 tablespoon</td>
</tr>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-09-20 12:32:38

请尝试:

代码语言:javascript
复制
$ing= 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon'
$output1 = explode(',', $ing);
$output=array();
foreach($output1 as $item){
    $output[]=explode(':', $item);
}

若要显示分段:

代码语言:javascript
复制
foreach($output as $row){
   //insert a new row here:
   echo '<tr>';
      //fill cells in row:
      echo '<td>'.$row[0].'</td>';
      echo '<td>'.$row[1].'</td>';
   //close the row:
   echo '</tr>';
}
票数 2
EN

Stack Overflow用户

发布于 2015-09-20 12:41:41

请试试这个..。这可能会奏效:)

代码语言:javascript
复制
$ing= 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon';
$output = explode(',', $ing);

function getBetween($content,$start,$end) {
    $r = explode($start, $content);

    if (isset($r[1])){

        $r = explode($end, $r[1]);
        return $r[0];

    }
    return '';
}


    foreach ($output as $value) {
        # code...

            $value = $value.'_!!_';
            $data = getBetween($value,':','_!!_');
            echo $data = str_replace('_!!_','',$data);
            echo '<br>';
    }
票数 0
EN

Stack Overflow用户

发布于 2015-09-20 18:53:21

代码语言:javascript
复制
$ing = 'coconut water :3-4 cups, coconut flesh : ½ cup, tea :4 tablespoon, Ice cubes :24-32, Lemon juice :4 tablespoon, Honey :8 tablespoon';

$lines = explode( ',', $ing );

foreach( $lines as $line ) {
    $item = explode( ':', $line );
    $tablerow[] = '<td>' . $item[0] . '</td><td>' . $item[1] . '</td>';
}

echo '<table><tr>' . implode( '</tr><tr>', $tablerow ) . '</tr></table>';
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32679383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档