Getting the first character of a string with PHP

Published on 2021-11-12 • Modified on 2021-11-12

In this snippet, we see how to get the first character of a string with PHP and Symfony. In the first part, we see how to use the array access notation. We must be careful because it won't work with special characters. In this case, we have to use the mb_substr function. In the second part, we see how to use the Symfony string component. It automatically handles everything: special characters, empty strings and null values.


<?php

declare(strict_types=1);

// src/Controller/Snippet/Snippet173Trait.php

namespace App\Controller\Snippet;

use function Symfony\Component\String\u;

/**
 * I am using a PHP trait to isolate each snippet in a file.
 * This code should be called from a Symfony controller extending AbstractController (as of Symfony 4.2)
 * or Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
 * Services are injected in the main controller constructor.
 */
trait Snippet173Trait
{
    public function snippet173(): void
    {
        echo "โ€”โ€” Array access โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”\n";

        $str = 'ABCD';
        echo 'Case nยฐ1 : ';
        echo $str[0]; // A
        echo "\n";

        $str = 'รคBCD';
        echo 'Case nยฐ2 : ';
        echo $str[0]; // ๏ฟฝ => Doesn't work because รค is a special character
        echo "\n";

        $str = 'รคBCD';
        echo 'Case nยฐ3 : ';
        echo mb_substr($str, 0, 1); // รค => OK using mb_substr with a special character
        echo "\n";

        //$str = '';
        //echo 'Case nยฐ4 : ';
        //echo $str[0]; // PHPStan error: Offset 0 does not exist on ''.
        //echo "\n";

        //$str = null;
        //echo 'Case nยฐ5 : ';
        //echo $str[0]; // PHPStan error: Offset 0 does not exist on null.
        //echo "\n";

        echo "\n";
        echo "โ€”โ€” With the Symfony string component โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”โ€”\n";

        $str = u('ABCD');
        echo 'Case nยฐ6 : ';
        echo $str->slice(0, 1); // A
        echo "\n";

        $str = u('รคBCD');
        echo 'Case nยฐ7 : ';
        echo $str->slice(0, 1); // รค => The Symfony component automatically hanldes special characters
        echo "\n";

        $str = u('');
        echo 'Case nยฐ8 : ';
        echo $str->slice(0, 1); // "" => returns an empty string in this case
        echo "\n";

        $str = u(null);
        echo 'Case nยฐ9 : ';
        echo $str->slice(0, 1); // "" => also returns an empty string in this case

        // That's it! ๐Ÿ˜
    }
}

 Run this snippet  โ‰ช this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test โ‰ซ  More on Stackoverflow   Read the doc  More on the web

<?php

declare(strict_types=1);

namespace App\Tests\Controller\Snippets;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use function Symfony\Component\String\u;

/**
 * @covers Snippet173Trait
 */
final class Snippet173Test extends KernelTestCase
{
    /**
     * @covers Snippet173Trait::snippet173
     */
    public function testSnippet173(): void
    {
        $str = 'ABCD';
        self::assertSame('A', $str[0]);

        $str = 'รคBCD';
        self::assertNotSame('รค', $str[0]);

        $str = 'รคBCD';
        self::assertSame('รค', mb_substr($str, 0, 1));

        $str = u('ABCD');
        self::assertSame('A', $str->slice(0, 1)->toString());

        $str = u('รคBCD');
        self::assertSame('รค', $str->slice(0, 1)->toString());

        $str = u('');
        self::assertSame('', $str->slice(0, 1)->toString());

        $str = u(null);
        self::assertSame('', $str->slice(0, 1)->toString());
    }
}