Skip to content

Commit b972ae4

Browse files
Parsing NOTE values. Also implemented escaping of newline characters according to RFC2425 section 5.8.4.
2 parents 15d11cd + ef692ec commit b972ae4

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ protected function parse()
255255
$cardData->photo = $value;
256256
}
257257
break;
258+
case 'NOTE':
259+
$cardData->note = $this->unescape($value);
260+
break;
258261
}
259262
}
260263
}
@@ -305,4 +308,16 @@ protected function parseAddress($value)
305308
);
306309
}
307310

311+
/**
312+
* Unescape newline characters according to RFC2425 section 5.8.4.
313+
* This function will replace escaped line breaks with PHP_EOL.
314+
*
315+
* @link http://tools.ietf.org/html/rfc2425#section-5.8.4
316+
* @param string $text
317+
* @return string
318+
*/
319+
protected function unescape($text)
320+
{
321+
return str_replace("\\n", PHP_EOL, $text);
322+
}
308323
}

tests/VCardParserTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ public function testUrl()
153153
$this->assertEquals($parser->getCardAtIndex(0)->url['PREF;WORK'][0], 'http://work1.example.com');
154154
$this->assertEquals($parser->getCardAtIndex(0)->url['PREF;WORK'][1], 'http://work2.example.com');
155155
}
156+
157+
public function testNote()
158+
{
159+
$vcard = new VCard();
160+
$vcard->addNote('This is a testnote');
161+
$parser = new VCardParser($vcard->buildVCard());
162+
163+
$vcardMultiline = new VCard();
164+
$vcardMultiline->addNote("This is a multiline note\nNew line content!\r\nLine 2");
165+
$parserMultiline = new VCardParser($vcardMultiline->buildVCard());
166+
167+
$this->assertEquals($parser->getCardAtIndex(0)->note, 'This is a testnote');
168+
$this->assertEquals(nl2br($parserMultiline->getCardAtIndex(0)->note), nl2br("This is a multiline note" . PHP_EOL . "New line content!" . PHP_EOL . "Line 2"));
169+
}
156170

157171
public function testTitle()
158172
{

0 commit comments

Comments
 (0)