【发布时间】:2011-03-12 19:24:35
【问题描述】:
我正在创建一个格式工具,用于从文章中剥离内容以供打印。演示可以看到here。完整的来源是可用的here。
现在工具条格式化并且还可以使用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 文本框 = "嗨,简
今天过得怎么样
希望一切都好”;
大多数文本将来自电子邮件和其他来源,基本上需要超级性别。
【问题讨论】:
标签: php regex preg-replace trim nl2br