将文本格式设置为向左
我正在创建一个格式工具,可以从要打印的文章中删除内容。可以在此处查看演示。完整来源可在此处获取。
现在,该工具会剥离格式,还可以使用 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 文本框 =“嗨,简
你今天过得怎么样
希望一切顺利”;
大多数文本将来自电子邮件和其他来源,基本上它需要具有超级性别特征。
I am creating a format tool that strips content from articles for print. The demo can be seen here. The full source is avaliable here.
Right now the tool strips formating and can also keep paragraphs by using nl2br
What I would like to do is to be able to shift the content to the left and only have a paragraph if there is a break between the content.
for example:
This
is
the
first
paragraphSecond Paragraph
Becomes:
this is the first paragraph
Second Paragraph
I tried this by using regex to check if there were two spaces at the end but that didn't work. Here is some sample code:
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;
Any help on this would be great
EDIT: include example
POST textbox = "Hi Jane
How are You doing today
I hope all is well";
Most text will be coming from e-mails and other sources,basicly it needs to be super genderic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的正则表达式是
/(?
将其替换为空格。
更新
一个小修正,
The regex you need is,
/(?<!\r\n)\r\n(?=\w)/
replace it with a space.
Update
A small correction,
你可以写这个
Your can write this