0

我正在创建一个格式工具,可以从文章中删除内容以供打印。演示可以在这里看到。完整的源代码可在此处获得。

现在工具条格式化并且还可以通过使用来保留段落nl2br我想做的是能够将内容向左移动,并且如果内容之间有中断,则只有一个段落。

例如:


是 第一段
_ _

第二段

变成:

这是第一段

第二段

我尝试使用正则表达式检查末尾是否有两个空格,但这不起作用。这是一些示例代码: HTML:

<form method="post" action="">
    <textarea cols="68" rows="21" name="textinput"></textarea><br/>
    <input type="checkbox" name="keep_paragraphs" value="true" checked /> Keep Paragraphs<br/>
    <input type="checkbox" name="shift_left" value="true" /> Remove whitespace after line unless it ends in two spaces<br/>
    <input type="submit" value="Submit" />
    </form>

PHP:

$text= $_POST['textinput'];
        $p= $_POST['keep_paragraphs'];
        $lb= $_POST['shift_left'];
        if(get_magic_quotes_gpc()){
        $text = stripslashes($text);
        // strip off the slashes if they are magically added.
        }
        $text = htmlentities($text);
        //if we should keep formatting
        if($p=="true"){
            $text =nl2br($text);
        }
        if($lb=="true"){
            $text = preg_replace('/\s+/', ' ', trim($text));
        }
echo $text;

对此的任何帮助都会很棒

编辑:包括示例

POST 文本框 = “嗨,简

你今天怎么样

我希望一切都好”;

大多数文本将来自电子邮件和其他来源,基本上它需要超级性别。

4

2 回答 2

1

你可以写这个

$text = preg_replace('@\n([a-z])@Us', ' \1', trim($text));
于 2011-03-12T19:36:07.360 回答
1

您需要的正则表达式是,

/(?<!\r\n)\r\n(?=\w)/

用空格替换它。

更新

一个小小的修正,


$text ="This
is
a
paragraph  

Second Paragraph";

$lb = "true";
if($lb=="true"){
            $text2 = preg_replace('/(?<!\r\n)\r\n(?=\w)/', ' ', trim($text));

        }

echo $text2;
于 2011-03-12T22:14:56.567 回答