Warm tip: This article is reproduced from serverfault.com, please click

showing only first letter or paragraph

发布于 2020-11-29 11:02:36

can anyone helping me to showing first name/word on navigation? i've trying this code

<div class="d-sm-none d-lg-inline-block">Welcome, <?php $string = $this->session->userdata('name'); echo $firstCharacter = $string[0]; ?></div></a>

but this code only showing the first character. i mean like if my name george bush then it only get "Welcome, g". i want to showing more like "Welcome, george" without bush. how to do it?

sorry for my bad english and i'm new to php things. and thanks for helping me before

Questioner
IshaMadway
Viewed
0
Sven 2020-11-29 19:13:21

You could use regular expressions, like this:

echo preg_replace('/^([^\s]+)\s*.*$/', '$1', $string);

or split it:

echo (explode(' ', $string))[0];

or 'tok' it:

echo strtok($string, " ")