如何将逗号分隔的字符串拆分为数组并再次用冒号拆分该数组?
How can I split a comma delimited string into an array and again split that array by colon?
我有一个字符串,用逗号分隔。我已经使用 explode 用逗号分隔它,这给了我数组形式的结果。在那个数组中,我有需要用 :
分隔的数据。我怎样才能做到这一点?下面是字符串和输出。
字符串:
$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);
输出:
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 杯冒号。
$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);
}
输出:
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
)
)
期望的输出:
<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>
如果您在使用 explode 后遍历数组,您也可以使用 explode 以冒号分隔值(数组的值)。
请尝试:
$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);
}
要显示段:
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>';
}
请试试这个..这可能有用:)
$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>';
}
$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>';
我有一个字符串,用逗号分隔。我已经使用 explode 用逗号分隔它,这给了我数组形式的结果。在那个数组中,我有需要用 :
分隔的数据。我怎样才能做到这一点?下面是字符串和输出。
字符串:
$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);
输出:
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 杯冒号。
$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);
}
输出:
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
)
)
期望的输出:
<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>
如果您在使用 explode 后遍历数组,您也可以使用 explode 以冒号分隔值(数组的值)。
请尝试:
$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);
}
要显示段:
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>';
}
请试试这个..这可能有用:)
$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>';
}
$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>';