我有使用QR扫描仪Web应用程序提取的二维码内容。仅下面的示例。
示例:
// This is the string extracted from the QR Code
"Name1","Name2","Name3","Name4","Name5","Name6","Name7"
当我提交将其存储到数据库时,我已将其分配给变量
PHP
<?php
if(isset($_POST['submit'])) {
$qr_results = $_POST['qr_bookingresults'];
$array_results = ltrim($qr_results,"QR Code:"); // I used ltrim because there is a string QR Code: attached when I extract the qr code
echo $array_results;
}
?>
但是当我使用print_r function()
时,字符串仅存储在一个数组中,如下所示:
$fetchresult = array($array_results); // Assign variable and convert $array_results variable to an array
print_r($fetchresult);
print_r()函数结果
Array ( [0] => "Name1","Name2","Name3","Name4","Name5","Name6","Name7" )
我的问题是,为什么只将整个字符串存储到一个数组中?其中应该是这样。
Array ( [0] => Name1 [1] => Name2 [2] => Name3 [3] => Name4 [4] => Name5 [5] => Name6 [6] => Name7 )
因为我真正想做的是提取内容并将其存储到数组中,因此我可以自由地将其插入到mysql中,如下所示:
// Assign each variable to an array for inserting into mysql
$name1 = $fetchresult[0];
$name2 = $fetchresult[1];
$name3 = $fetchresult[2];
$name4 = $fetchresult[3];
$name5 = $fetchresult[4];
$name6 = $fetchresult[5];
$name7 = $fetchresult[6];
有人知道如何解决此问题吗?谢谢
答案 0 :(得分:1)
这似乎是CSV格式的字符串。
使用str_get_csv()
来确保您考虑到任何文本逗号和转义引号。
<?php
$qr_string = '"Name1","Name2","Name3","Name4","Name5","Name6","Name7","I am here to ruin your day, am I not?","24"" (inch) TV"';
$array = str_getcsv( $qr_string );
print_r( $array );
结果:
Array
(
[0] => Name1
[1] => Name2
[2] => Name3
[3] => Name4
[4] => Name5
[5] => Name6
[6] => Name7
[7] => I am here to ruin your day, am I not?
[8] => 24" (inch) TV
)
答案 1 :(得分:0)