我有一个这样的字符串:
$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);