为什么rtrim删除更多的字符,并给出奇怪的输出?

时间:2017-09-19 18:32:42

标签: php character-trimming

我正在尝试使用string删除rtrim中的最后几个字符。

我有字符串"Scryed (download torrent) - TPB"

我想要输出字符串"Scryed"

e.g。

$title_t = "Scryed (download torrent) - TPB";

echo ($title_t)  ;
echo "\n";
$title =  ( rtrim ($title_t, "(download torrent) - TPB") );

echo ($title)  ;

给出

Scryed (download torrent) - TPB
Scry

为什么? 预期产出

Scryed (download torrent) - TPB
Scryed

1 个答案:

答案 0 :(得分:5)

这是因为rtrim的第二个参数是字符的列表。不是要修剪的字符串!您应该使用substrstr_replace

$title =  substr($title_t, 0, strlen($title_t) - strlen("(download torrent) - TPB"));

$title = str_replace("(download torrent) - TPB", "" , $title_t);