How to replace specific text in PHP?

I have some data in the following format.

Orange - $3.00
Banana - $1.25

I would like to print this only as follows:

Orange
Banana

I tried using the following code in a loop, but the data may have 0 in other places. Therefore, if I can find 0 only at the end for a specific string.

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

Any ideas?

+3
source share
3 answers

Is this what you are looking for?

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

Or, if you want to process all the data:

<?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;
}
?>
+1
source

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

$n=explode(" - ", "Banana - $1.25");
print $n[0];
+1
source
$string = explode("-", $originalText);
$word = $string[0];

and here it is :)

0
source

All Articles