2017-09-04 101 views
0

我在嘗試使用字符串時遇到了問題,並且當我將其複製到記事本++並查看所有字符選項卡時,它顯示以下附加符號。我的知識是他們是換行符和空格。問題是,我似乎不能讓他們從我的字符串中刪除?清除CR CL和PHP字符串中的空格

說明:

我有一個使用了shell_exec搶從存儲的數據庫信息的功能。

$output = trim(shell_exec("'".$command."' 2>&1")); //Trimmed version 
return $output; 

我有一個信用系統,但是當他們加載網頁它調用函數來獲取根據用戶信用等

$Credits = Sqlite('select "Credits" from TBL WHERE User = "bla" limit 1'); 

事情是,信貸回來了,旁邊有一家 。所以如果我有9.50存儲,我收到�9.50。當看着這個,我注意到上面的字符包含在字符串中?

我的PHP嘗試:

$Credits = preg_replace('/\s/', '', $Credits); //Clear all spaces 
//$Credits = str_replace(' ', '', $Credits); //Clear spaces <-- dont work either 
$Credits = str_replace('\r\n', '', $Credits); //Clear all new lines 
echo $Credits; //Still returns the new line etc 

string issue

+0

請發佈[MCVE(最小完整可驗證示例)](http://stackoverflow.com/help/mcve)。 '$ Credits = preg_replace('/ \ s /','',$ Credits);'應該完成這項工作。那麼,如果有Unicode字符,你需要''/ \ s/u'',但這似乎並不是這種情況。另外,爲什麼不嘗試[**'trim()'**](http://php.net/manual/pl/function.trim.php)? –

+0

爲什麼你想刪除這些字符? – Philipp

+0

btw。你應該使用'「\ r \ n」'而不是''\ r \ n'' - 轉義序列只能在字符串中使用''' – Philipp

回答

0

$積分僅僅是一個變量,不會改變你的文件的內容。因此請試試看:

<?php 
$text = file_get_contents('source.txt'); 
echo '<pre>'; // to display any new line from linebreak 
echo $text; 

echo '<br>====<br>'; 

$text = preg_replace('/\r\n/','a',$text); // a is just an indicator of original linebreak which you can use '' empty instead 

echo $text // display text after linebreak is replaced from variable $text 
file_put_contents('source2.txt', $text); // save this to file with linebreaks removed 
// or replace content of source.txt 
?>