Skip to content

Commit 129006e

Browse files
committed
Multiline note parsing
1 parent 54a0827 commit 129006e

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/VCard.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public function buildVCard()
379379
$properties = $this->getProperties();
380380
foreach ($properties as $property) {
381381
// add to string
382-
$string .= $this->fold($property['key'] . ':' . $property['value'] . "\r\n");
382+
$string .= $this->fold($property['key'] . ':' . $this->escape($property['value']) . "\r\n");
383383
}
384384

385385
// add to string
@@ -490,6 +490,21 @@ protected function fold($text)
490490
// split, wrap and trim trailing separator
491491
return substr(chunk_split($text, 73, "\r\n "), 0, -3);
492492
}
493+
494+
/**
495+
* Escape newline characters according to RFC2425 section 5.8.4.
496+
*
497+
* @link http://tools.ietf.org/html/rfc2425#section-5.8.4
498+
* @param string $text
499+
* @return string
500+
*/
501+
protected function escape($text)
502+
{
503+
$text = str_replace("\r\n", "\\n", $text);
504+
$text = str_replace("\n", "\\n", $text);
505+
506+
return $text;
507+
}
493508

494509
/**
495510
* Get output as string

src/VCardParser.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ protected function parse()
256256
}
257257
break;
258258
case 'NOTE':
259-
$cardData->note = $value;
259+
$cardData->note = $this->unescape($value);
260260
break;
261261
}
262262
}
@@ -308,4 +308,8 @@ protected function parseAddress($value)
308308
);
309309
}
310310

311+
protected function unescape($text)
312+
{
313+
return str_replace("\\n", PHP_EOL, $text);
314+
}
311315
}

tests/VCardParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ public function testNote()
161161
$parser = new VCardParser($vcard->buildVCard());
162162

163163
$vcardMultiline = new VCard();
164-
$vcardMultiline->addNote("This is a multiline note\nNew line content!");
164+
$vcardMultiline->addNote("This is a multiline note\nNew line content!\r\nLine 2");
165165
$parserMultiline = new VCardParser($vcardMultiline->buildVCard());
166166

167167
$this->assertEquals($parser->getCardAtIndex(0)->note, 'This is a testnote');
168-
$this->assertEquals($parserMultiline->getCardAtIndex(0)->note, "This is a multiline note\nNew line content!");
168+
$this->assertEquals(nl2br($parserMultiline->getCardAtIndex(0)->note), nl2br("This is a multiline note" . PHP_EOL . "New line content!" . PHP_EOL . "Line 2"));
169169
}
170170

171171
public function testTitle()

0 commit comments

Comments
 (0)