VKA
VKA

Reputation: 319

How Do I 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 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?

Upvotes: 3

Views: 111

Answers (3)

Daniël Voogsgerd
Daniël Voogsgerd

Reputation: 600

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;
}
?>

Upvotes: 1

Avihay Menahem
Avihay Menahem

Reputation: 238

$string = explode("-", $originalText);
$word = $string[0];

and thats it :)

Upvotes: 0

Johnny Craig
Johnny Craig

Reputation: 5002

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

$n=explode(" - ", "Banana - $1.25");
print $n[0];

Upvotes: 1

Related Questions