猿问

PHP将字符串分开存储在数组中

我正在使用我的 PHP 从 mysql 数据库中获取数据。当我将两个文件名都存储在数据库中时,使用值将文件名存储在数组中时遇到问题。


这是我存储在 blob 中的内容:


draft_attachment.rar

draft_attachment - Copy.rar

这是代码:


$mailbox = $link->prepare("SELECT * FROM draft WHERE id = ?");

$mailbox->execute([$id]);


// set the resulting array to associative

$row = $mailbox->fetch(PDO::FETCH_ASSOC);

$attachments = array();


if($mailbox->rowCount() == 1)

{

    $attachment = array();

    $draft_to = htmlspecialchars($row['to_email']);

    $draft_subject = quoted_printable_decode($row['subject']);

    $draft_message = $row['message'];

    $attach[] = $row['attachments'];

}

?>

当我尝试这个时:


$attach[] = $row['attachments'];

它将两个字符串存储在数组中:


Array

{

   [0] => draft_attachment.rar

draft_attachment - Copy.rar

}

所以我试过这个:


$attachments = array();  

$i = 0;


if($mailbox->rowCount() == 1) {

    $attachments[$i] = $row['attachments'];

    $i++;

}

它不会单独存储字符串,所以我不知道该怎么做。


这是我想要实现的目标:


Array

{

   [0] => draft_attachment.rar

   [1] => draft_attachment - Copy.rar

}

你能告诉我一个例子,我如何让两个字符串分开,这样我就可以使用该值将它们存储在数组中?


谢谢你。


狐的传说
浏览 190回答 1
1回答

手掌心

preg_split您可以使用on \R(任何新行字符序列)拆分字符串(explode如果您知道确切的行结束字符,也可以使用):$attachments = preg_split('/\R/', $row['attachments']);3v4l.org 上的演示
随时随地看视频慕课网APP
我要回答