Php 仅当术语在其自己的行上时才删除该术语

Php 仅当术语在其自己的行上时才删除该术语,php,str-replace,Php,Str Replace,我有这个: $text = ' hello world    hello '; 如果它在自己的线路上,如何仅删除?因此,在上面的示例中,应该删除第二个。结果应该是: $text = ' hello world  hello '; 到目前为止我都试过了 通过str\u replace(),我可以: $text = str_replace(' ', '',

我有这个:

$text = '
   hello world
    
    hello
';
如果它在自己的线路上,如何仅删除
?因此,在上面的示例中,应该删除第二个
。结果应该是:

$text = '
   hello world

    hello
';
到目前为止我都试过了 通过
str\u replace()
,我可以:

$text = str_replace(' ', '', $text);

但这将删除
的所有实例,而不仅仅是当它在自己的行上时

我已经尝试过这种方法,我得到了您想要的输出

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

您可以分成几行,然后在只包含
和空格的行中用
替换空字符串

我们将保留原始的行尾,通过捕获它们

<?php

$text = '
   hello&nbsp;world
   &nbsp;
   &nbsp;hello
';
$lines = preg_split('@(\R)@', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach($lines as &$v)
    if (trim($v) === '&nbsp;')
        $v = str_replace('&nbsp;', '', $v);

$result = implode($lines);
var_dump($result);
我的做法是:

<?php
    $lines = explode("\n",$text);
    foreach($lines as $n => $l)
        if(trim($l) == '&nbsp;')
            $lines[$n] = str_replace('&nbsp;','',$l);
    $text = implode("\n",$lines);
?>
  • 在新行上分解文本
  • 修剪数组中的每个值
  • 用值清空每个数组项
  • 以新线内爆
  • 产生以下代码:

    $chunks = explode(PHP_EOL, $text);
    $chunks = array_map('trim', $chunks);
    foreach (array_keys($chunks, '&nbsp;') as $key) {
        $chunks[$key] = '';
    }
    $text = implode(PHP_EOL, $chunks);
    

    也许是这样的:

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

    (这适用于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);
    ?>
    

    如果您知道您的行尾字符,并且您的
    后面总是跟着一行新字:

    <?php
    $text = '
       hello&nbsp;world
       &nbsp;
       &nbsp;hello
    ';
    
    
    print str_replace("&nbsp;\n", "\n", $text);
    

    警告:任何以前面有其他内容的
    结尾的行也会受到影响,因此这可能无法满足您的需要。

    您可以为此使用正则表达式,使用DOTALL和多行修饰符以及环视断言:

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


    到目前为止,我已经尝试过的美丽的添加-应该是这个网站的标准+1@misorude请随时用您的方法发布答案。@OfirBaruch我尝试了
    $text=str_replace('\n'.\n'.'','.$text)但不起作用@FlashThunder为什么不使用
    str_replace()
    而不是判断我的经验来发布答案呢@misorude,但如果你觉得这是一个站点范围内的问题,你需要在meta上与社区讨论这个问题。为什么要将其推到这里?代码的输出与所询问的内容不同:/“`hello world hello``最初的要求是只将
    替换为空文本,而不远程处理整行:/。另外,请记住,不是我否决了你的答案:)我的评论只是一条友好的信息:)现在好多了:)我测试了它,但不起作用:)。还要记住,我没有投任何反对票。这只是一个友好的评论:)在测试您的解决方案后,输出保持不变。@MerianosNikos它怎么不呢??这就是我在本地运行您的代码时得到的::)@MerianosNikos哦,很有趣,在PHP5上工作,但在PHP7上不工作。@MerianosNikos我刚找到PHP7错误了吗?隐马尔可夫模型。。。真奇怪,应该可以。如果我们坚持使用
    str\u replace()
    ,这实际上是一个非常可靠的解决方案@Progrock是否可以对其进行调整,使其在
    之后的空格计数?例如:
    “\n”
    @henrikpeterson nothis也将删除空白,作者只想删除
    @FlashThunder我所拥有的作为输出的内容与问题所要求的输出完全一致。如果您检查我的输出是否与初始要求相同:)是否尝试在PHP7中运行我的代码?你有其他的输出吗?我想说,作者甚至没有意识到其中有一个标签/几个空格,因为他在其他评论中要求后面有空格(并且没有包含空格)。@FlashThunder我知道。我只是想问一个例子。哦,对不起,这是个误会
    preg_replace("~(?sm)(?<=\n)\s*&nbsp;(?=\n)~", '',$text);
    
    >>> $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
       """
    >>>