开发者

iCalendar TEXT Data Type - Preparing Values with PHP

I am providing download links for my application's events. One piece I am missing is how to prepare values for the TEXT data type. Specifically, the value that will end up with the DESCRIPTION property has HTML. I have downloaded several PHP projects related to the iCalendar RFC, but I haven't tracked down a good snippet of code for preparing TEXT values.


I found a blog entry that was only available in Google Cache that had line wrapping, and I noticed that generating an ICS file from Outlook 2010 has line wrapping, but I'm going to leave it alone for now and come back to it if there's a problem.

I also decided to add support for the different possible line endings:

/**
 * Prepare data for a TEXT开发者_如何学JAVA field
 *
 * @param string $text
 * @return string
 */
public static function prepareText($text)
{
    $search = array('\\', ';', ',', "\r\n", "\n", "\r");
    $replace = array('\\\\', '\;', '\,', '\n', '\n', '\n');
    return str_replace($search, $replace, $text);
}


Note the mixture of single and double quotes for the line break (Double quotes interpret the line breaks whereas single ones don't)

$text = 'Hello, World!
This is in a new line; after a semicolon/comma';
$search = array('/',';',',',"\N","\n");
$replace = array('\/','\;','\,','\n','\n');
$description = str_replace($search,$replace,$text);

print_r($description);

For sure this can be done with a probably easier RegExp..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜