-2

我有一个这样的字符串:

$string = 'Product Name | 43.39';

我想把它分成两个变量

$productName

$productPrice
4

4 回答 4

3

你也可以这样做

list($productName, $productPrice) = explode(' | ', $string);

几乎一样,但我喜欢一个班轮:)

于 2012-04-16T18:33:09.073 回答
1

您可以为此使用爆炸功能

$string = 'Product Name | 43.39';
$array = explode(' | ',$string);
$productName = $array[0]; //will echo Product Name
$productPrice = $array[1]; //will echo 43.39

这个函数基本上把你的字符串和它看到分隔符的地方分开。

一个较短的版本基本上是:

$string = 'Product Name | 43.39';
list($productName, $productPrice) = explode(' | ', $string);

它做的事情完全相同,只是在一行上,可能更容易阅读。

于 2012-04-16T18:31:38.200 回答
1

较短的版本:

$string = 'Product Name | 43.39';
list($productName,$productPrice) = explode(' | ',$string);
于 2012-04-16T18:33:25.387 回答
1

尝试

$string = 'Product Name | 43.39';
list($productName , $productPrice) = explode(" | ",$string);
于 2012-04-16T18:38:40.973 回答