PHP code example of pleb / vcardio

1. Go to this page and download the library: Download pleb/vcardio library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pleb / vcardio example snippets


$vCardsCollection = Pleb\VCardIO\VCardParser::parseFile('./contacts.vcf');

// OR

$vCardsRawData = 'BEGIN:VCARD
VERSION:4.0
FN:Jeffrey Lebowski
BDAY:19421204
X-MAIN-HOBBY:Bowling
END:VCARD';

$vCardsCollection = Pleb\VCardIO\VCardParser::parseRaw($vCardsRawData);

Pleb\VCardIO\VCardsCollection {
    vCards: [
        Pleb\VCardIO\Models\VCardV40 {
            version: "4.0",
            relevantData: {
                version: "4.0",
                fn: "Jeffrey Lebowski",
                bday: DateTimeImmutable @-854466859,
                x: {
                    mainHobby: "Bowling"
                }
            },
            fn: [
                {
                    value: "Jeffrey Lebowski",
                    attributes: []
                }
            ],
            bday: {
                dateTime: DateTimeImmutable @-854466859,
                format: "Ymd",
                formatted: "19421204",
                extactYear: true,
                attributes: []
            },
            x: [
                {
                    name: "main-hobby",
                    formattedName: "mainHobby",
                    value: "Bowling",
                    attributes: []
                }
            ],
            ...
        },
    ],
}

$vCardsCollection = (new Pleb\VCardIO\VCardsCollection())
    ->addVCard($vCard1)
    ->addVCard($vCard2);

// OR

$vCardsCollection = new Pleb\VCardIO\VCardsCollection([$vCard1, $vCard2]);

foreach($vCardsCollection as $vCard){
    // ...
}
// OR
$vCard = $vCardsCollection->first();
// OR
$vCard = $vCardsCollection->getVCard(0); // 1,2,...

$vCard->getFullName();                      // :?string
$vCard->getLastName();                      // :?string
$vCard->getFirstName();                     // :?string
$vCard->getEmails();                        // :array<string>
$vCard->getPhones();                        // :array<string>
$vCard->getX('main-hobby', multiple:false); // :?string|array
$vCard->getX('main-hobby', multiple:true);  // :array
// ...

Pleb\VCardIO\Models\VCardV30 {
    version: '3.0'
    // ...,
    agent: Pleb\VCardIO\Models\VCardV30 {
        version: "3.0"
        // ...
    },
    // ...
}

$vCard = Pleb\VCardIO\VCardBuilder::make()
    ->fullName('Jeffrey Lebowski')
    ->nickName('The Dude')
    ->birthday(new DateTime('1942-12-04'))
    ->x('main-hobby', 'Bowling')
    ->get();

echo nl2br((string) $vCard);

echo nl2br((string) $vCardsCollection);

$vCard->export('./file/export/destination.vcf');

// OVERWRITTEN
$vCardCollection->export('./file/export/destination.vcf', append:false); 

// APPENDED
$vCardCollection->export('./file/export/destination.vcf', append:true);