2010-07-12 128 views

回答

5

你想刪除空格?嘗試trim()。它的親戚ltrimrtrim可能更接近你想要的。

+0

謝謝我認爲rtrim做到了。 – usertest 2010-07-12 02:18:04

0

如果您正在尋找刪除的最後一行是包含換行符(\ n)的字符串,那麼你會做這樣的事情...

$someString = "This is a test\nAnd Another Test\nAnd another test."; 

echo "SomeString BEFORE=".$someString."\n"; 

// find the position of the last occurrence of a \n 
$firstN = strlen($someString) - strpos(strrev($someString), "\n"); 

// get rid of the last line 
$someString = substr($someString, 0, $firstN); 

echo "SomeString AFTER=".$someString; 
0

,如果你想刪除從文件末尾開始,可以嘗試使用PHP的file()函數讀取文件(將其放入數組中),然後彈出最後一個元素。這是假設PHP正在識別文件中的行尾。

0
$subject = 'Text 
written 
with 
lines'; 

echo preg_replace('/\\n[^\\n]*\\n?$/', '', $subject); 

用功能,可以有在其中被忽略結束一個特殊的換行,「因爲它往往是讓一個在文件的最後一個好習慣(假設你從文件中讀取字符串)。