PHP code example of dev-lancer / minecraft-motd-parser

1. Go to this page and download the library: Download dev-lancer/minecraft-motd-parser 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/ */

    

dev-lancer / minecraft-motd-parser example snippets


$formatCollection = \DevLancer\MinecraftMotdParser\Collection\FormatCollection::generate();
$colorCollection  = \DevLancer\MinecraftMotdParser\Collection\ColorCollection::generate(true); //When set to true, colors from BE are added
$parser = new \DevLancer\MinecraftMotdParser\Parser\TextParser($formatCollection, $colorCollection, '&');

$motd = "A &l&fMine&4craft &rServer";
$motdItemCollection = $parser->parse($motd, new \DevLancer\MinecraftMotdParser\Collection\MotdItemCollection());

$formatCollection = \DevLancer\MinecraftMotdParser\Collection\FormatCollection::generate();
$colorCollection  = \DevLancer\MinecraftMotdParser\Collection\ColorCollection::generate();
$parser = new \DevLancer\MinecraftMotdParser\Parser\ArrayParser($formatCollection, $colorCollection);

$motd = [
    [ "text" => "A "],
    [
        "bold" => true,
        "extra" => [
            [
                "color" => "white",
                "text" => "Mine"
            ],
            [
                "color" => "dark_red",
                "text" => "craft "
            ],
        ]
    ],
    [
        "text" => "Server"
    ]
];
$motdItemCollection = $parser->parse($motd, new \DevLancer\MinecraftMotdParser\Collection\MotdItemCollection());

$motd = "A &l&fMine&f&lcraft &rServer";
$motdItemCollection = $parser->parse($motd, new \DevLancer\MinecraftMotdParser\Collection\MotdItemCollection());

//Output before
[
    ['text': "A "],
    ['bold': true, 'color': "white", 'text': "Mine"],
    ['bold': true, 'color': "white", 'text': "craft "],
    ['reset': true, 'text': "Server"],
]

$motdItemCollection->mergeSimilarItem();
//Output after
[
    ['text': "A "],
    ['bold': true, 'color': "white", 'text': "Minecraft "],
    ['reset': true, 'text': "Server"],
]

$parser = new \DevLancer\MinecraftMotdParser\Parser\TextParser();
$motd = "A §l§fMine§4craft §rServer";
$motdItemCollection = $parser->parse($motd, new \DevLancer\MinecraftMotdParser\Collection\MotdItemCollection());

$generator = new \DevLancer\MinecraftMotdParser\Generator\HtmlGenerator();

// Generate HTML from the MOTD item collection
echo $generator->generate($motdItemCollection); 

$generator = new \DevLancer\MinecraftMotdParser\Generator\RawGenerator("§");
// Generate raw text from the MOTD item collection
echo $generator->generate($motdItemCollection); 
//output: A §f§lMine§4craft §rServer

$generator = new \DevLancer\MinecraftMotdParser\Generator\TextGenerator();
// Generate plain text from the MOTD item collection
echo $generator->generate($motdItemCollection); 
//output: A Minecraft Server

class CustomBoldFormatter implements FormatterInterface
{
    public function getKey(): string
    {
        return 'l';
    }

    public function getName(): string
    {
        return 'bold';
    }

    public function getFormat(): string
    {
        return '<b class="CustomBoldFormatter">%s</b>';
    }
}

// Create a new format collection
$formatCollection = \DevLancer\MinecraftMotdParser\Collection\FormatCollection::generate();

// and override the default formatter for bold
$formatCollection->add(new CustomBoldFormatter());

// Create a new MOTD item
$motdItem = new \DevLancer\MinecraftMotdParser\MotdItem();
$motdItem->setBold(true);
$motdItem->setText("Hello World");

// Create a new MOTD item collection and add the MOTD item
$motdItemCollection = new \DevLancer\MinecraftMotdParser\Collection\MotdItemCollection();
$motdItemCollection->add($motdItem);

// Generate HTML using the custom formatter
$generator = new \DevLancer\MinecraftMotdParser\Generator\HtmlGenerator($formatCollection);
echo $generator->generate($motdItemCollection);