亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

刪除多個空格

刪除多個空格

藍山帝景 2019-10-06 13:20:50
我$row['message']從MySQL數據庫獲取數據,我需要刪除所有空白\n \t,依此類推。$row['message'] = "This is   a Text \n and so on \t     Text text.";應格式化為:$row['message'] = 'This is a Text and so on Text text.';我試過了: $ro = preg_replace('/\s\s+/', ' ',$row['message']); echo $ro;但不會刪除\n或\t,而只能刪除單個空格。誰能告訴我該怎么做?
查看完整描述

4 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

你需要:


$ro = preg_replace('/\s+/', ' ',$row['message']);

您使用的\s\s+是指空格(空格,制表符或換行符)后跟一個或多個空格。這實際上意味著用單個空格替換兩個或多個空格。


您想要的是用單個空格替換一個或多個空格,因此您可以使用模式  \s\s*或\s+(推薦)


查看完整回答
反對 回復 2019-10-06
?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

<?php

$str = "This is  a string       with

spaces, tabs and newlines present";


$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str);


echo $str;

echo "\n---\n";

echo "$stripped";

?>

這個輸出


This is  a string   with

spaces, tabs and newlines present

---

This is a string with spaces, tabs and newlines present


查看完整回答
反對 回復 2019-10-06
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

簡化為一個功能:


function removeWhiteSpace($text)

{

    $text = preg_replace('/[\t\n\r\0\x0B]/', '', $text);

    $text = preg_replace('/([\s])\1+/', ' ', $text);

    $text = trim($text);

    return $text;

}

根據Danuel O'Neal的回答。


查看完整回答
反對 回復 2019-10-06
  • 4 回答
  • 0 關注
  • 712 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號