代码之家  ›  专栏  ›  技术社区  ›  Gal

PHP过滤字符串输入,截断所有换行符,1个字符

  •  0
  • Gal  · 技术社区  · 16 年前

    <br /><br />

    I am a
    
    
    
    
    SPaMmEr!
    

    I am a
    SPaMmEr!
    

    I am a
    .
    .
    .
    .
    SPaMmEr!
    

    4 回复  |  直到 16 年前
        1
  •  2
  •   soulmerge    16 年前

    $lines = explode("\n", str_replace(array("\r\n", "\r"), "\n", $string));
    

    preg_grep() :

    $lines = preg_grep('/^.{2}/', $lines);
    

    preg_match_all('/^.{2}.*$/', $string, $matches);
    $lines = $matches[0];
    

    $string = preg_replace('/^.?$\n/m', '', $string);
    
        2
  •  0
  •   Ben Everard    16 年前

    这个怎么样:

    $str = str_replace(array("\n", "\r"), '', $str);
    

    其中$str是您的用户输入。

    编辑 :

        3
  •  0
  •   Femaref    16 年前

        4
  •  0
  •   Ivan Nevostruev    16 年前

    ## replace 1 or more new line symbols with single new line symbol
    $empty_lines_trimmed = preg_replace('"(\r?\n)+"', "\n", $empty_lines);
    
    推荐文章