Robert Jones
Robert Jones

Reputation: 408

PHP get the strings separate to store in the array

I am working on my PHP to fetch the data from the mysql database. I have got a problem with storing the filename in the array using the values when I have stored both filenames in a database.

Here is what I have stored in the blob:

draft_attachment.rar
draft_attachment - Copy.rar

Here is the code:

$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'];
}
?>

When I try this:

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

It will store both strings in the array:

Array
{
   [0] => draft_attachment.rar
draft_attachment - Copy.rar
}

So I have tried this:

$attachments = array();  
$i = 0;

if($mailbox->rowCount() == 1) {
    $attachments[$i] = $row['attachments'];
    $i++;
}

It wont store the strings separate so I dont know what to do.

Here is what I want to achieve:

Array
{
   [0] => draft_attachment.rar
   [1] => draft_attachment - Copy.rar
}

Can you please show me an example how I could get both strings to go separate so I could be able to store them in the array using the value?

Thank you.

Upvotes: 0

Views: 78

Answers (2)

Mohan Bade
Mohan Bade

Reputation: 26

you can use explode function, something like explode("\n", $row['attachments'])

Upvotes: 0

Nick
Nick

Reputation: 147146

You can split the string using preg_split on \R (any sequence of new line characters) (explode can also work if you know the exact line ending characters):

$attachments = preg_split('/\R/', $row['attachments']);

Demo on 3v4l.org

Upvotes: 1

Related Questions