问题
I'm creating an ICS file dynamically with PHP which is "mostly" working except for one snag:
In the DESCRIPTION area of the ICS file, I want to concatentate several database fields and separate them into their own paragraphs so when the user opens the calendar entry, they're not all concatentated as one string.
My problem is that if I try and concatenate these fields using the new line characters, the "\n" isn't showing up as part of the DESCRIPTION content. I've opened a valid ICS file that accomplishes this task and the file shows:
DESCRIPTION:1. Click this link to join the Webinar:\n\n https://www1.site.com/join/444/444444\n\n\n2. Choose one of the following audio options:\n\n
The "\n" is part of the file content. But if I try and generate it, it treats the \n as not content, but the end of the line of the ICS line:
DESCRIPTION:line1
line2
So the ICS can't be read and isn't valid.
I've tried many combinations, but can't seem to get this working. Seems like it should be simple, but isn't. Any ideas?
回答1:
The icalendar DESCRIPTION field uses two separate characters, '\' followed by 'n' to represent line breaks. If you're writing a PHP string literal with those characters, you can either use a string with single quotes and simply write \n
, or use double quotes and escape the backslash so that \n
isn't turned into a newline character. For example:
$description = 'This is a description\nwith a newline';
// Or
$description = "This is a description\\nwith a newline";
Exactly how you'll end up doing it depends on your source code, which we've not seen.
来源:https://stackoverflow.com/questions/20336179/ics-file-using-php-want-to-insert-new-line-characters-to-break-up-description