清除CR CL和PHP字符串中的空格

问题描述:

我在尝试使用字符串时遇到了问题,并且当我将其复制到记事本++并查看所有字符选项卡时,它显示以下附加符号。我的知识是他们是换行符和空格。问题是,我似乎不能让他们从我的字符串中删除?清除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

$积分仅仅是一个变量,不会改变你的文件的内容。因此请试试看:

<?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 
?>