3

I have some data in the following format.

Orange - $3.00
Banana - $1.25

I would like to print this as only as follows:

Orange
Banana

I tried using following code in a loop but the data may have 0 in other places as well. So if I can find 0 only at the end for a specific line.

$pos=strpos($options, "0");
$options = substr($options, 0, $pos)."\n";

Any ideas?

4

3 回答 3

1

Are you trying to print only the name of the item and not the price?

$n=explode(" - ", "Banana - $1.25");
print $n[0];
于 2012-05-14T17:47:06.347 回答
1

Is this what you're looking for?

<?php
$input = 'Orange - $3.00';
list($fruit, $price) = explode('-', $input);
?>

Or if you want to proces all the 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;
}
?>
于 2012-05-14T17:48:22.240 回答
0
$string = explode("-", $originalText);
$word = $string[0];

and thats it :)

于 2012-05-14T17:47:06.330 回答