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

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

僅當術語位于其自己的行上時才將其刪除

僅當術語位于其自己的行上時才將其刪除

PHP
千萬里不及你 2022-09-17 17:49:49
我有這個:$text = '   hello world        hello';如何刪除它,只有當它在自己的線上?因此,在上面的例子中,第二個應該被刪除。結果應為:  $text = '   hello world    hello';到目前為止,我嘗試過什么通過,我可以:str_replace()$text = str_replace(' ', '', $text);但是,這將刪除 的所有實例,而不僅僅是當它位于自己的行上時。 
查看完整描述

5 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

我已經嘗試過這種方法,我得到了你想要的輸出


// Your initial text

$text = '

   hello world

    

    hello

';


// Explode the text on each new line and get an array with all lines of the text

$lines = explode("\n", $text);


// Iterrate over all the available lines

foreach($lines as $idx => $line) {

    // Here you are free to do any if statement you want, that helps to filter

    // your text.


    // Make sure that the text doesn't have any spaces before or after and 

    // check if the text in the given line is exactly the same is the  

    if ( ' ' === trim($line) ) {

        // If the text in the given line is   then replace this line 

        // with and emty character

        $lines[$idx] = str_replace(' ', '', $lines[$idx]);

    }

}


// Finally implode all the lines in a new text seperated by new lines.

echo implode("\n", $lines);

我在本地的輸出是這樣的:



hello world


 hello


查看完整回答
反對 回復 2022-09-17
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

我的方法是:

  1. 在新行上分解文本

  2. 修剪數組中的每個值

  3. 清空每個具有值的數組項 

  4. 用新線內爆

生成以下代碼:

$chunks = explode(PHP_EOL, $text);

$chunks = array_map('trim', $chunks);

foreach (array_keys($chunks, ' ') as $key) {

    $chunks[$key] = '';

}

$text = implode(PHP_EOL, $chunks);


查看完整回答
反對 回復 2022-09-17
?
慕工程0101907

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

也許是這樣的:


$text = preg_replace("~(^[\s]?|[\n\r][\s]?)( )([\s]?[\n\r|$])~s","$1$3",$text);

http://sandbox.onlinephpfunctions.com/code/f4192b95e0e41833b09598b6ec1258dca93c7f06


(這適用于 PHP5,但在某些版本的 PHP7 上卻不起作用)


替代方案是:


<?php

    $lines = explode("\n",$text);

    foreach($lines as $n => $l)

        if(trim($l) == '&nbsp;')

            $lines[$n] = str_replace('&nbsp;','',$l);

    $text = implode("\n",$lines);

?>


查看完整回答
反對 回復 2022-09-17
?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

如果您知道行尾字符,并且您的行后始終跟著一個新行:&nbsp;


<?php

$text = '

   hello&nbsp;world

   &nbsp;

   &nbsp;hello

';



print str_replace("&nbsp;\n", "\n", $text);

輸出(此處的格式設置中丟失了一些初始空格):


   hello&nbsp;world


   &nbsp;hello

警告:任何以其他內容結尾的行也會受到影響,因此這可能不能滿足您的需求。&nbsp;


查看完整回答
反對 回復 2022-09-17
?
慕娘9325324

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

為此,您可以使用正則表達式,將 DOTALL 和多行修飾符與環視斷言結合使用:

preg_replace("~(?sm)(?<=\n)\s*&nbsp;(?=\n)~", '',$text);
  • (?sm): 多點 (s) 多線 (m)

  • (?<=\n):換行符之前(不是匹配項的一部分)

  • \s*&nbsp;\s*: 單次具有可選的周圍空格

  • (?=\n):尾隨換行符(不是匹配項的一部分)

>>> $text = '                                                                                               

 hello&nbsp;world                                                                                         

 &nbsp;                                                                                                   

 &nbsp;hello                                                                                           

 ';

=> """

   \n

      hello&nbsp;world\n

      &nbsp;\n

      &nbsp;hello\n

   """

>>> preg_replace("~(?sm)(?<=\n)\s*&nbsp;\s*(?=\n)~", '',$text);

=> """

   \n

      hello&nbsp;world\n

   \n

      &nbsp;hello\n

   """

>>>


查看完整回答
反對 回復 2022-09-17
  • 5 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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