如何使用php修剪字符串中的换行符和空格

时间:2013-05-10 12:27:43

标签: php

我有一个字符串如下。

$key="bf3iZzS9vJFf68R2DgKcVEb0gG2ZuxB2N3nx/VmCgnvQweTiPhdGIWgoifR/a2Cj7yhD6RnAlqPD
vXFXubDdIyuoEEf9FbAN4z7uuUBbuJWfy8uNPG/9b2fi6uU8d/jPtkPz+QotFGxf68R2DgKcVAxl
n+JT2sq20MHk4j4XRiFoKIn0f2tgo+sVQZmUH8fjjct7YYgqWIoUUZ48kBq4e4V1i7vZuywrLDDy
eF89tBO3AlP4UvcRhPpnSvkiAr2L00RIQ3Q3WcWoeJyfwbWuBeZMGdVMHWKfw71xV7mw3SPRPkWp
raBIMl/rxHYOApxUzm3JG/E8ydOJnxwk5AfZ3eM+7rlAW7iVXWtOiYlc5kHdM/qPEzgJoFj+dIyF
D7oY0MHk4j4XRiFoKIn0f2tgoxwGU/9s1ZR1twJT+FL3EYS9PVOO+F2kl2UXr7fBbMqTw71xV7mw
3SNNL4XmU21tkydbUbRMxn4N9F+0umQpKZtf68R2DgKcVA3zA29A+MLjuU1CMmtq/Jq3AlP4UvcR
hL/yQjyWXBag4z7uuUBbuJWCcAMY+XQUuv5r4BXsS5TEjct7YYgqWIp5fUCN1Hf0wISyuFeeMvzQ
O2BeYyz5Kd3+RkIizk8EmY3Le2GIKliKA/dDXrCS43zDvXFXubDdI7oJe5Kb6D8e4z7uuUBbuJXI
4FlDbp01GWfi6uU8d/jPS1i6Qoti3wtoJoL9ZE4l6rEu1ZiKp4vJA99VbSP4sJ1f68R2DgKcVIBW
KDaHLUsEbUQvCQrD81aFdYu72bssKy7QYbHRrCz3g3+dKCEUkrCNy3thiCpYit1ulMgQNuDb1Qzx
E7+eLjknb04gNyPt4cO9cVe5sN0j0T5Fqa2gSDJf68R2DgKcVM5tyRvxPMnTiZ8cJOQH2d3jPu65
QFu4lV1rTomJXOZBluDaFQ4uX+/XPw2JVlR1QNDB5OI+F0YhaCiJ9H9rYKOnLy436D/vb7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Gmfi6uU8d/jPJNYHn8pC3lQazz11SZ0l+IV1i7vZuywr5DbUic7ksoxFhlcfmsJGHDgakBQdAtaYOxSS96MvMLrl8OEMpKE9RtXITsXTj/JicJoSJbY+w3Mszce1vHs0YkdCZug6BRRxLM3Htbx7NGL91Op3oo9u0Jb8jdQb2jNB0MHk4j4XRiEuvf9jqO9829DB5OI+F0YhE8ZmQzVYvJYyVocMjBLRdf75AUVejP7gcKxceCT8ZcPQ9eUPjjCRynCsXHgk/GXDNMMfkG5pf9r+a+AV7EuUxD/rQwey6ePEV0FCHDgHtZK5KmPKJOYJlVl/JlDlxq+MSvmkp1o/5v4SsPAX5jO5KQ=="

我尝试过以下方法。

str_replace("\n", "", $key);

但它无效。

有什么建议吗??

由于

4 个答案:

答案 0 :(得分:2)

TRY

$order   = array("\r\n", "\n", "\r");
$replace = '';     
$final = str_replace($order, $replace, $str);
var_dump($final);

注意:由于str_replace()从左到右替换,因此在执行多次替换时,它可能会替换先前插入的值。所以先处理\r\n,这样就不会转换两次。

答案 1 :(得分:1)

试试这个,我认为这是一种简单的方式

 echo str_replace(PHP_EOL, '', $key);

答案 2 :(得分:0)

试试这个:

echo str_replace("\r\n","", $key);

答案 3 :(得分:-1)

尝试以下代码

str_replace(chr(13), "", $key);

愿这会帮到你