2011-06-13 44 views
1

我有这段文字保存在一个变量:mindfuq上:星期天,2011年6月12日埋猎物约翰·桑福德埋PreyIn埋猎物,这个最新的约翰·桑福德通过删除与爆炸功能的最后一行

共享戏剧特色是他最喜欢的主角,坏人的警察“猎手”卢卡斯达文波特,他从过去重新审视了一个冷酷的案件。他早年在职业生涯中遇到过的案件的受害者发现了
分享给朋友:| | |艺术 - 文学故事,RSS Feeds和Widgets通过Feedzilla。

我想删除此文本的最后一行。我怎样才能做到这一点explodestr_replace不适合我。

+2

你为什么要用'explode()'做呢? – binaryLV 2011-06-13 07:08:08

+2

顺便说一句,这在哪里解析XML? – deceze 2011-06-13 07:17:24

+1

如果他们中的任何一个人为你工作,你应该考虑标记欺骗者或我的回答正确。 – Dan 2011-06-18 23:36:59

回答

9
$string = substr($string, 0, strrpos($string, "\n")); 

如果你绝对要使用explode

$string = join("\n", array_slice(explode("\n", $string), 0, -1)); 

两种方法假定,有没有最后的行后尾随"\n",看到@ binaryLV的评论。

+4

我会在'strpos()'调用中用'rtrim($ string)'替换'$ string'。这样,如果有任何尾随的'\ n',那也无关紧要。尽管'\ r \ n'可能有一些问题,所以在调用substr()之前,可能需要添加'$ string = str_replace(“\ r \ n”,“\ n”,$ string)''。 – binaryLV 2011-06-13 07:11:28

0

如果内容不包含其他换行符,使用strstr()

$line = strstr(trim($text), "\n", true); // last arg true, to return before needle 

如果将有多个换行,并希望以编程方式删除线:

$lines = explode("\n", trim($text)); 
unset($lines[count($lines) - 1]); // removes last line