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);
namespace App\Controller\Snippet;
use App\Helper\String\StringHelper;
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.
*
* @property StringHelper $stringHelper
*/
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";
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]; // PHP Warning. PHPStan error: Offset 0 does not exist on ''. @phpstan-ignore-line
echo "\n";
$str = null;
echo 'Case nยฐ5 : ';
echo @$str[0]; // PHP Warning. PHPStan error: Offset 0 does not exist on null. @phpstan-ignore-line
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
echo "\n\n";
echo "โโ With my Symfony string component helper โโโโโโโโโโโโโโโโโโโ\n";
echo 'Case nยฐ10 : ';
echo $this->stringHelper->u('ABCD')->firstChar(); // "A"
echo "\n";
echo 'Case nยฐ11 : ';
echo $this->stringHelper->u('รคBCD')->firstChar(); // "รค"
echo "\n";
echo 'Case nยฐ12 : ';
echo $this->stringHelper->u('')->firstChar(); // ""
echo "\n";
echo 'Case nยฐ13 : ';
echo $this->stringHelper->u(null)->firstChar(); // ""
// this firstChar() function is a custom function I have added (blog post to come)
// 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\Integration\Controller\Snippets;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use function Symfony\Component\String\u;
/**
* @see Snippet173Trait
*/
final class Snippet173Test extends KernelTestCase
{
/**
* @see 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());
}
}