Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get "first" AND "last" character of a php string in one line?

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'));

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  • 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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading