输出字符串的最后几个字符 - PHP / Yii

Output the last few characters of a string - PHP / Yii

如何只输出下面 docno 的最后 3 个字符?这个 docno 输出一串很长的数字,我想通过只显示最后 3 位数字来简化它以使其看起来更清晰。

Yii::$app->reporter->col((isset($data[0]['docno'])? $data[0]['docno']:''),'300',null,false,'1px solid ','','L','Helvetica','11','','','').'<br />';

谢谢。

使用substr()

如文档所述:

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If start is negative, the returned string will start at the start'th character from the end of string.

所以在第二个参数中使用负值

$data[0]['docno'] = substr($data[0]['docno'], -3);

现在在您的 Yii::$app->..... 行中使用它

您可以在第二个参数中使用带有负数的 substr()

$shortenedDocNo = substr($data[0]['docno'], -3);

来自PHP documentation:

string substr ( string $string , int $start [, int $length ] )

If start is negative, the returned string will start at the start'th character from the end of string.