试图切断最后一个空格的角色

Vel*_*tar 1 php

我需要找出字符串中的最后一个字符是charachter是一个空格的位置,并将其剪切掉.这是我正在使用的函数,但if语句似乎有问题,但我无法弄清楚是什么.$ text-string中肯定有一个空格.

假设我有一个字符串"嘿,我的名字是约翰".然后它必须缩短为"嘿,我的名字是".

$checkLastChar = false;
$text = $line[2];

while($checkLastChar != true){
   for($i = 1; $i <= strlen($text); $i++)
    {
        if($text[strlen($tekst) - $i] == " ") {
                $checkLastChar = true;
            $text = substr($text, 1, strlen($text) - $i);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Nat*_*ate 5

substr($string, 0, strrpos($string, ' '));
Run Code Online (Sandbox Code Playgroud)