我有一个这样的字符串:
$string = 'Product Name | 43.39';
我想把它分成两个变量
$productName
和
$productPrice
你也可以这样做
list($productName, $productPrice) = explode(' | ', $string);
几乎一样,但我喜欢一个班轮:)
您可以为此使用爆炸功能。
$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);
它做的事情完全相同,只是在一行上,可能更容易阅读。
较短的版本:
$string = 'Product Name | 43.39';
list($productName,$productPrice) = explode(' | ',$string);
尝试
$string = 'Product Name | 43.39';
list($productName , $productPrice) = explode(" | ",$string);