php 乱码的文本格式

php text formatting for a messy text

我想格式化文本,但我不知道该怎么做。

我有文字:

Row1
Row2
Title
text   _    text


text   _    text
Row1
Row2
Title
text   _    text


text   _    text

我想修改在

Title
text : text
text : text

Title
text : text
text : text

我想删除第 1 行和第 2 行,但上面保留一个空行。

删除正文与正文之间空的两行

并变换_ in :

我尝试使用 str_replace 和 preg_replace 但我不知道该怎么做。

    <?php
        if(isset($_POST["submit"])) {
            $text = $_POST["text-ed"];
            $change = '  _   ';
            $change_r = ' : ';
            $remove_line = array("\n", "\r");

$rezult  = preg_replace('/[\n\r]+/', '', $text);
$rezult  = str_replace(array($remove_line, $change), array('', $change_r), $text);
            }
    ?>

尝试使用此代码:

<?php 

$text = "Row1
Row2
Title
text   _    text


text   _    text
Row1
Row2
Title
text   _    text


text   _    text";

var_dump($text);


// Delete rows
$text = str_replace(array("Row1","Row2"), "", $text); 
// Replace the underscore
$text = str_replace("_", ":", $text); 
// Replace all duplicate space
$text = preg_replace('/ +/', ' ',$text); 
// Replace all duplicate newlines
$text = preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n", $text); 

var_dump($text);
?>

结果:

string(103) "Row1
Row2
Title
text   _    text


text   _    text
Row1
Row2
Title
text   _    text


text   _    text"


string(60) "
Title
text : text
text : text
Title
text : text
text : text"