0

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?

0

2 Answers 2

5

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"
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

what if we don't have idea how long string may be, and how many parts we may have ?
@Ans - you should count() or sizeof() array and get the number of elements

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.