-1

Is there a one line way to combine getting first and last character of a string?

I tried (without success):

$title = "IncredibleTitle";
$title_characters = (mb_substr($title, 0, 1, 'UTF8')) && (mb_substr($title, 0, -1, 'UTF8'));
Pepe
  • 960
  • 9
  • 21

1 Answers1

1
  • Use . period character for concatenation.
  • To get last character, 2nd param has to be -1 and third param will be value 1 or the length. See mb_substr.

Snippet:

<?php

$title = "IncredibleTitle";
$title_characters = (mb_substr($title, 0, 1, 'UTF8')) . (mb_substr($title, -1, 1, 'UTF8'));
echo $title_characters;

Online Demo

nice_dev
  • 17,053
  • 2
  • 21
  • 35