2011-03-12 94 views
0

我正在创建一个格式化工具,用于从要打印的文章中去除内容。演示可以看到here。完整的源代码可用here向左格式化文本

现在工具条格式化,也可以通过使用nl2br保留段落我想要做的是能够将内容左移,只有在内容之间有中断时才有段落。

例如:

This
is
the
first
paragraph

Second Paragraph

变为:

this is the first paragraph

Second Paragraph

我用正则表达式来检查是否有结尾处有两个空格,但没有奏效尝试这个。下面是一些示例代码: 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 textbox = "Hi Jane

How are You doing today

I hope all is well";

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

回答

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; 
+0

这似乎失败:http://codepad.org/N2dZ1GSP – BandonRandon 2011-03-12 23:44:45

+0

更正,它现在必须工作。 – 2011-03-13 05:20:07

+0

它似乎在键盘上工作,但不是在我的实际软件上是'htmlentities','stripslashes'或'nl2br'去做格式化和打破preg_replace? – BandonRandon 2011-03-13 06:19:17

1

你可以这样写

$text = preg_replace('@\n([a-z])@Us', ' \1', trim($text)); 
+0

这似乎这么想的要解决的问题:http://codepad.org/3dGnuIQN – BandonRandon 2011-03-12 21:04:16

+0

对不起,解决这个问题 – azat 2011-03-12 21:44:23

+0

如果我不要误认在文本前加了2个空格http://codepad.org/htGXDmuC – BandonRandon 2011-03-12 22:08:04