duanliyi5997 2015-01-13 02:26
浏览 76
已采纳

在PHP中创建的vCard中没有出现预期的换行符

I have the following code that I use to generate a vCard (note that variables are declared earlier)

BEGIN:VCARD
VERSION:4.0
N:<?php echo $person["last"]; ?>;<?php echo $person["first"];?>;;;
FN:<?php echo $person["first"];?> <?php echo $person["last"]; ?>
ORG:<?php echo str_replace(",",'\,',Site::$NAME);?>
TITLE:CPA
TEL;TYPE=work,voice;VALUE=uri:tel:+1-<?php echo Site::$PHONE; ?>
TEL;TYPE=work,fax;VALUE=uri:tel:+1-<?php echo Site::$FAX; ?>
ADR;TYPE=work;LABEl="<?php echo Site::$ADDRESS . '
' . Site::$LINE_TWO . '
' . Site::$CITY . '\,' . Site::$STATE . " " . Site::$ZIP; ?>":;<?php echo Site::$LINE_TWO; ?>;<?php echo Site::$ADDRESS; ?>;<?php echo Site::$CITY; ?>;<?php echo Site::$STATE; ?>;<?php echo Site::$ZIP; ?>;United States of America
EMAIL:<?php echo $person["email"]; ?>
END:VCARD

But the problem is that I do not get new lines where I expect. Example output:

BEGIN:VCARD
VERSION:4.0
N:LAST;FIRST;;;
FN:FIRST LASTORG:ORGANIZATIONTITLE:TITLE
TEL;TYPE=work,voice;VALUE=uri:tel:PHONE_NUMBERTEL;TYPE=work,fax;VALUE=uri:PHONE_NUMBERADR;TYPE=work;LABEl="ADDRESS":;ADDRESS
EMAIL:EMAILEND:VCARD

For what ever reason some of the newlines are there, and some of them are never outputted. Is there a reason why this is happening, and is there a way to fix it?


Edit: I set the following headers prior to any output:

header("Content-Type: text/vcard");
header('Content-Disposition: attachment; filename="'. $person["first"] . " " . $person["last"] . ".vcf" . '"');
  • 写回答

1条回答 默认 最新

  • douhandie6615 2015-01-15 22:09
    关注

    The reason for this is that php removes newlines after the php closing tag ?>. There are several ways to work around this.

    • you could for example append a blank space after the closing tag
    • or add an additional newline after the closing tag
    • or you could echo the whole vcard data using as newlines, instead of opening and closing php everytime
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗