Reputation: 1
So I've got a string being added via imap in a PHP script. There's always 12 lines in the email - always. I need to be able to assign each line to a variable.
Usually, I'd use some markers to signify the end/start of the value from the email but that's not an option in this case (they're being sent from ships and so a larger email means more money).
The email looks like -
CCL21045
04/03/2020
1800
32.50
17.20
Nil
Nil
It's all sent as one block with no white space in between the lines. I need to get the 4 lines above into their own variables.
Any help would be appreciated!
Upvotes: 0
Views: 1025
Reputation: 1906
Best Way to do this.
$getListAArr= explode(PHP_EOL, $email);
print_r($getListAArr);
// Output
[
"CCL21045",
"04/03/2020",
"1800",
"32.50",
"17.20",
"Nil",
"Nil",
]
Upvotes: 0
Reputation: 38436
If you know that the format will always be in the same order, you could use PHP's explode()
to split the input by lines into individual variables. Something like:
[$report_date, $coordinates, $speed, $destination, ] = explode("\n", $email);
Even if it's not in a specific order, you can still use explode()
to split the input into an array and loop over each line to match it to what you want/need. i.e.:
$lines = explode("\n", $email);
foreach ($lines as $line) {
if (preg_match('@^\d{2}/\d{2}\d{4}$@', $line)) {
$report_date = $line;
}
// ... more rules for other input types
}
Upvotes: 1