Why is rtrim removing more characters, and giving weird output?

北城余情 提交于 2020-01-14 06:08:28

问题


I am trying to remove last few characters from the string using rtrim.

i have string "Scryed (download torrent) - TPB"

i want output string "Scryed"

e.g.

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

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

echo ($title)  ;

gives

Scryed (download torrent) - TPB
Scry

Why is that ? expected output is

Scryed (download torrent) - TPB
Scryed

回答1:


It's because rtrim's second parameter is list of characters. Not a string to be trimmed! You should use a substr or str_replace:

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

or

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


来源:https://stackoverflow.com/questions/46307527/why-is-rtrim-removing-more-characters-and-giving-weird-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!