将文本格式设置为向左

发布于 2024-10-21 18:51:53 字数 1698 浏览 6 评论 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 文本框 =“嗨,简

你今天过得怎么样

希望一切顺利”;

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

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
paragraph

Second 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪 2024-10-28 18:51:53

您需要的正则表达式是

/(?

将其替换为空格。

更新

一个小修正,


$text ="This
is
a
paragraph  

Second Paragraph";

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

        }

echo $text2;

The regex you need is,

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

replace it with a space.

Update

A small correction,


$text ="This
is
a
paragraph  

Second Paragraph";

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

        }

echo $text2;
醉态萌生 2024-10-28 18:51:53

你可以写这个

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

Your can write this

$text = preg_replace('@\n([a-z])@Us', ' \1', trim($text));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文