New lines not showing as expected in vCard created in php

I have the following code which I am using to create a vCard (note that the variables were 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 . '\n' . Site::$LINE_TWO . '\n' . 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 am not getting newlines where I expect. Output example:

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 some reason, some of the newlines are there, and some are never printed. Is there a reason why this is happening and is there a way to fix it?


Edit . I am setting the following headers before any output:

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

      

+3


source to share


1 answer


The reason for this is that php removes newlines after the php close tag ?>

. There are several ways to get around this.



  • you could for example add an empty space after the closing tag
  • or add an extra newline after the closing tag
  • or you could echo

    use all vcard data with \n

    as new lines instead of opening and closing php every time
+1


source







All Articles