【发布时间】:2016-03-24 15:58:36
【问题描述】:
我的字符串 $podcast->title 返回类似这样的内容:
Artist Name - The Title
我正在使用以下 2 行代码:
$this_dj = substr($podcast->title, 0, strpos($podcast->title, "-"));
$this_dj = substr($this_dj, 0, -1);
第一行删除了之后的所有内容(包括“-”),这让我留下了:
Artist Name
第二行去掉末尾的空格。
我的问题是,我可以将这两行合并为一行吗?
我试过了:
$this_dj = substr($podcast->title, 0, strpos($podcast->title, "-"), -1);
但这没有用。
【问题讨论】:
-
如果你想在最后删除空格,最好的方法是使用
trim()。 -
我可以在最后添加吗?
-
像这样:
trim(substr($podcast->title, 0, strpos($podcast->title, "-")));