Talon
Talon

Reputation: 4995

Split a String into Two Variables By Separator

I have a string that looks like this

$string = 'Name of product | 34';

I want to break it into two new strings that'll be this:

$productName = 'Name of product';
$productPrice = '34';

What is the best way to do that?

Upvotes: 0

Views: 932

Answers (2)

Elen
Elen

Reputation: 2343

$newarray = explode(" | ", $string);
echo $newarray[0]; //Name of product
echo $newarray[1]; //34

Upvotes: 4

user142162
user142162

Reputation:

You can do this quite simply using list() and explode():

list($productName, $productPrice) = explode(" | ", $string);
$productName    string(15) "Name of product"
$productPrice   string(2) "34"

Upvotes: 5

Related Questions