我有以下格式的一些数据。
Orange - $3.00
Banana - $1.25
我只想打印如下:
Orange
Banana
我尝试在循环中使用以下代码,但数据在其他地方也可能为 0。因此,如果我只能在特定行的末尾找到 0。
$pos=strpos($options, "0");
$options = substr($options, 0, $pos)."\n";
有什么想法吗?
最佳答案
这是您要找的吗?
<?php
$input = 'Orange - $3.00';
list($fruit, $price) = explode('-', $input);
?>
或者如果你想处理所有的输入:
<?php
$input = 'Orange - $3.00
Banana - $1.25';
$fruitlist = array();
$sepLines = explode("\n", $input);
foreach($seplines as $line)
{
list($fruit, $price) = explode(' - ', $line);
$fruitlist[] = $fruit;
}
?>
关于php - 如何替换 PHP 中的特定文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10588358/