如何在空格后添加换行符?
<?php
$trxinfo = "R# VGG20.085760253978 SUCCESS. SN/Ref: 6131718556/CODE=QV5TP-U9U8W-BGUPM-65N53-D8Y52. Price 18,800. Amount 1,943,778 - 18,800 = 1,924,978. Date 04/10 10:36:15";
$myArray = preg_split('/[\s.]+/', $trxinfo);
print_r($myArray);
?>
输出结果为:
Array ( [0] => R# [1] => VGG20 [2] => 085760253978 [3] => SUCCESS [4] => SN/Ref: [5] => 6131718556/CODE=QV5TP-U9U8W-BGUPM-65N53-D8Y52 [6] => Price [7] => 18,800 [8] => Amount [9] => 1,943,778 [10] => - [11] => 18,800 [12] => = [13] => 1,924,978 [14] => Date [15] => 04/10 [16] => 10:36:15 )
我该怎么办?添加\n
答案 0 :(得分:2)
如果您不介意,在这种情况下您也可以使用explode()
。例如:
$trxinfo = "R# VGG20.085760253978 SUCCESS. SN/Ref: 6131718556/CODE=QV5TP-U9U8W-BGUPM-65N53-D8Y52. Price 18,800. Amount 1,943,778 - 18,800 = 1,924,978. Date 04/10 10:36:15";
$myArray = implode("\n", explode('. ', $trxinfo));
echo nl2br($myArray);
应该是这样的:
R# VGG20.085760253978 SUCCESS
SN/Ref: 6131718556/CODE=QV5TP-U9U8W-BGUPM-65N53-D8Y52
Price 18,800
Amount 1,943,778 - 18,800 = 1,924,978
Date 04/10 10:36:15
答案 1 :(得分:0)
查看实时Code
<?php
$string = "R# VGG20.085760253978 SUCCESS. SN/Ref: 6131718556/CODE=QV5TP-U9U8W-BGUPM-65N53-D8Y52. Price 18,800. Amount 1,943,778 - 18,800 = 1,924,978. Date 04/10 10:36:15";
$myArray = preg_split('/[\s\n]+/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
$myArray = array_map('ltrim', $myArray);
echo '<pre>'.print_r($myArray, true);
?>
Array
(
[0] => R#
[1] => VGG20.085760253978
[2] => SUCCESS.
[3] => SN/Ref:
[4] => 6131718556/CODE=QV5TP-U9U8W-BGUPM-65N53-D8Y52.
[5] => Price
[6] => 18,800.
[7] => Amount
[8] => 1,943,778
[9] => -
[10] => 18,800
[11] => =
[12] => 1,924,978.
[13] => Date
[14] => 04/10
[15] => 10:36:15
)