PHP 自动套用段落格式标签

发布于 2024-10-21 02:45:43 字数 690 浏览 7 评论 0原文

这是一个相当基本的问题,但我只找到了三个答案。我想对可能包含也可能不包含其他 html 元素的未格式化内容进行自动段落标记。两个不错的函数是 wordpress wpautopHTMLPurifier AutoFormat.AutoParagraph 以及最后一些随机的addParagraphsNew。然而,第一个与我的应用程序许可证不兼容,第二个不在段落标签内添加换行符,第三个是不稳定的。

有谁知道商业上合适的许可脚本,允许将具有换行符和双换行符的html内容转换为

标签?

我相信 HTMLPurifier 选项可能是我侵入 AutoFormat.AutoParagraph 插件以使其添加换行符的最佳方法,但我希望能稍微简单一些。我知道很懒。

This is a fairly basic question however I've only ever managed to find three answers. I want to autoparagraph unformatted content that may or may not contain other html elements. The two good to go functions are the wordpress wpautop, HTMLPurifier AutoFormat.AutoParagraph and lastly some random addParagraphsNew. However the first is incompatible with my applications license, the second doesn't add line breaks within the paragraph tags and the third is flakey.

Does anyone know on a commercially suitable licensed script that allows html content that has line breaks and double line breaks to be converted into <br /> and <p> tags?

I believe the HTMLPurifier option is probably the best way to go my hacking into the AutoFormat.AutoParagraph plugin to get it to add line breaks but I was hoping was something slightly easier. Lazy I know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

暮色兮凉城 2024-10-28 02:45:43

好吧,你可以自己破解它(如果我正确理解你需要什么),

$text = "hello, I am text

and this is another paragraph, please do some cool
stuff with this (this is after a  line break)

last apragrahp...";

$text = str_replace("\r\n","\n",$text);

$paragraphs = preg_split("/[\n]{2,}/",$text);
foreach ($paragraphs as $key => $p) {
    $paragraphs[$key] = "<p>".str_replace("\n","<br />",$paragraphs[$key])."</p>";
}

$text = implode("", $paragraphs);

echo $text;

这实际上会输出:

<p>hello, I am text</p><p>and this is another paragraph, please do some cool<br />stuff with this (this is after a  line break)</p><p>last apragrahp...</p>

注意它现在如何丢失所有换行符等......

well you could hack it yourself (if I understand what you need correctly)

$text = "hello, I am text

and this is another paragraph, please do some cool
stuff with this (this is after a  line break)

last apragrahp...";

$text = str_replace("\r\n","\n",$text);

$paragraphs = preg_split("/[\n]{2,}/",$text);
foreach ($paragraphs as $key => $p) {
    $paragraphs[$key] = "<p>".str_replace("\n","<br />",$paragraphs[$key])."</p>";
}

$text = implode("", $paragraphs);

echo $text;

That will actually output this:

<p>hello, I am text</p><p>and this is another paragraph, please do some cool<br />stuff with this (this is after a  line break)</p><p>last apragrahp...</p>

Notice how it's missing all the newlines etc. now...

月竹挽风 2024-10-28 02:45:43

这是一个 JavaScript 解决方案,它将向行添加段落标签。它将忽略空行、标题、图像、列表和代码块内的行。

function Add_paragraph_tags(input)
{
    console.log('Adding paragraph tags');
    var lines = input.split("\n");
    line_is_inside_code_block = false;
    line_is_inside_pre_block = false;
    line_is_inside_table = false;
    line_is_inside_comment = false;
    line_is_inside_video = false;
    line_is_inside_css_block = false;

    lines.forEach
    (
            function(line, index)
            {
                    lines[index] = lines[index].replace(/\<h[1-9]\>\<\/h[1-9]\>/,''); //remove instances of <h2></h3>
                    lines[index] = lines[index].replace(/\<(h[1-9]\>)(.*)\<\/h[1-9]\>/,'<$1$2</$1');
                    if(
                            line==''
                            || /^[ |\t]*<h/.test(line)
                            || /^[ |\t]*<a/.test(line)
                            //|| /^[ |\t]*<p/.test(line)
                            || /^[ |\t]*<br/.test(line)
                            || /^[ |\t]*<img/.test(line)
                            || /^[ |\t]*<\/?div/.test(line)
                            || /^[ |\t]*<pre/.test(line)
                            || /^[ |\t]*<code/.test(line)
                            || /^[ |\t]*<table/.test(line)
                            || /^[ |\t]*<iframe/.test(line)
                            || /^[ |\t]*<figure/.test(line)
                            || /^[ |\t]*<figcaption/.test(line)
                            || /^[ |\t]*<li>/.test(line)
                            || /^[ |\t]*<ol>/.test(line)
                            || /^[ |\t]*<\/ol>/.test(line)
                            || /^[ |\t]*<ul>/.test(line)
                            || /^[ |\t]*<\/ul>/.test(line)
                            || /^[ |\t]*<blockquote><p>/.test(line)
                            || /^[ |\t]*<video/.test(line)
                            || /^[ |\t]*<style/.test(line)
                            || /^[ |\t]*<!--/.test(line)
                            || line_is_inside_code_block
                            || line_is_inside_pre_block
                            || line_is_inside_table
                            || line_is_inside_comment
                            || line_is_inside_video
                            || line_is_inside_css_block
                    )
                    {
                            //do nothing
                    }
                    else
                    {
                            lines[index] = '<p>' + line + '</p>';
                            lines[index] = lines[index].replace('<p><blockquote>','<blockquote><p>');
                            lines[index] = lines[index].replace('</blockquote></p>','</p></blockquote>');
                            lines[index] = lines[index].replace(/(\<p\>)+/,'<p>'); //elimlnate multiple p tags
                            lines[index] = lines[index].replace(/(\<\/p\>)+/,'</p>'); //elimlnate multiple closing p tags
                            lines[index] = lines[index].replace(/\<p\>\<\/p\>/,''); //elimlnate <p></p>
                    }

                    /*if(lines[index+1].startsWith('<br'))
                    {
                            lines[index] = lines[index].replace(/\<\/p\>)/,'');
                    }*/

                    if(line.endsWith('</code>'))
                    {
                            line_is_inside_code_block = false;
                    }
                    else if(line.endsWith('</pre>'))
                    {
                            line_is_inside_pre_block = false;
                    }
                    else if(line.endsWith('</table>'))
                    {
                            line_is_inside_table = false;
                    }
                    else if(line.endsWith('-->'))
                    {
                            line_is_inside_comment = false;
                    }
                    else if(line.endsWith('/video>'))
                    {
                            line_is_inside_video = false;
                    }
                    else if(line.endsWith('/style>'))
                    {
                            line_is_inside_css_block = false;
                    }
                    else if(line.startsWith('<code'))
                    {
                            line_is_inside_code_block = true;
                    }
                    else if(line.startsWith('<pre'))
                    {
                            line_is_inside_pre_block = true;
                    }
                    else if(line.startsWith('<table'))
                    {
                            line_is_inside_table = true;
                    }
                    else if(line.startsWith('<!--'))
                    {
                            line_is_inside_comment = true;
                    }
                    else if(line.startsWith('<video'))
                    {
                            line_is_inside_video = true;
                    }
                    else if(line.startsWith('<style'))
                    {
                            line_is_inside_css_block = true;
                    }
            }
    );
    var output = lines.join('\n');
    output = output.replace(/\n\n+/g,'\n\n');
    output = output.replace(/\<\/p\>\n\<br/g,'\n<br');
    output = output.replace(/\<i\>/g,'<em>');
    output = output.replace(/\<\/i\>/g,'</em>');
    return output;
}

$('button').click(
  function() {
var input_text = $('#input').val();
var output_text = Add_paragraph_tags(input_text);
$('#output').val(output_text);
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<textarea id="input">
This line should have paragraph tags.

<pre>This is code and does not need paragraph tags</pre>

<em>This line should have paragraph tags.</em>

<h1>This is a header and it does not need paragraph tags</h1>

<blockquote>This is a blockquote. This line needs a paragraph tag.

Paragraph tags are needed here too.</blockquote>

<ul>
<li>This is a list item. It does not need a paragraph tag</li>
</ul>

</textarea>

<textarea id="output"></textarea>

<button type="button">Add paragraph tags</button>

Here is a JavaScript solution that will add paragraph tags to lines. It will ignore lines that are empty, headers, images, lists and within code blocks.

function Add_paragraph_tags(input)
{
    console.log('Adding paragraph tags');
    var lines = input.split("\n");
    line_is_inside_code_block = false;
    line_is_inside_pre_block = false;
    line_is_inside_table = false;
    line_is_inside_comment = false;
    line_is_inside_video = false;
    line_is_inside_css_block = false;

    lines.forEach
    (
            function(line, index)
            {
                    lines[index] = lines[index].replace(/\<h[1-9]\>\<\/h[1-9]\>/,''); //remove instances of <h2></h3>
                    lines[index] = lines[index].replace(/\<(h[1-9]\>)(.*)\<\/h[1-9]\>/,'<$1$2</$1');
                    if(
                            line==''
                            || /^[ |\t]*<h/.test(line)
                            || /^[ |\t]*<a/.test(line)
                            //|| /^[ |\t]*<p/.test(line)
                            || /^[ |\t]*<br/.test(line)
                            || /^[ |\t]*<img/.test(line)
                            || /^[ |\t]*<\/?div/.test(line)
                            || /^[ |\t]*<pre/.test(line)
                            || /^[ |\t]*<code/.test(line)
                            || /^[ |\t]*<table/.test(line)
                            || /^[ |\t]*<iframe/.test(line)
                            || /^[ |\t]*<figure/.test(line)
                            || /^[ |\t]*<figcaption/.test(line)
                            || /^[ |\t]*<li>/.test(line)
                            || /^[ |\t]*<ol>/.test(line)
                            || /^[ |\t]*<\/ol>/.test(line)
                            || /^[ |\t]*<ul>/.test(line)
                            || /^[ |\t]*<\/ul>/.test(line)
                            || /^[ |\t]*<blockquote><p>/.test(line)
                            || /^[ |\t]*<video/.test(line)
                            || /^[ |\t]*<style/.test(line)
                            || /^[ |\t]*<!--/.test(line)
                            || line_is_inside_code_block
                            || line_is_inside_pre_block
                            || line_is_inside_table
                            || line_is_inside_comment
                            || line_is_inside_video
                            || line_is_inside_css_block
                    )
                    {
                            //do nothing
                    }
                    else
                    {
                            lines[index] = '<p>' + line + '</p>';
                            lines[index] = lines[index].replace('<p><blockquote>','<blockquote><p>');
                            lines[index] = lines[index].replace('</blockquote></p>','</p></blockquote>');
                            lines[index] = lines[index].replace(/(\<p\>)+/,'<p>'); //elimlnate multiple p tags
                            lines[index] = lines[index].replace(/(\<\/p\>)+/,'</p>'); //elimlnate multiple closing p tags
                            lines[index] = lines[index].replace(/\<p\>\<\/p\>/,''); //elimlnate <p></p>
                    }

                    /*if(lines[index+1].startsWith('<br'))
                    {
                            lines[index] = lines[index].replace(/\<\/p\>)/,'');
                    }*/

                    if(line.endsWith('</code>'))
                    {
                            line_is_inside_code_block = false;
                    }
                    else if(line.endsWith('</pre>'))
                    {
                            line_is_inside_pre_block = false;
                    }
                    else if(line.endsWith('</table>'))
                    {
                            line_is_inside_table = false;
                    }
                    else if(line.endsWith('-->'))
                    {
                            line_is_inside_comment = false;
                    }
                    else if(line.endsWith('/video>'))
                    {
                            line_is_inside_video = false;
                    }
                    else if(line.endsWith('/style>'))
                    {
                            line_is_inside_css_block = false;
                    }
                    else if(line.startsWith('<code'))
                    {
                            line_is_inside_code_block = true;
                    }
                    else if(line.startsWith('<pre'))
                    {
                            line_is_inside_pre_block = true;
                    }
                    else if(line.startsWith('<table'))
                    {
                            line_is_inside_table = true;
                    }
                    else if(line.startsWith('<!--'))
                    {
                            line_is_inside_comment = true;
                    }
                    else if(line.startsWith('<video'))
                    {
                            line_is_inside_video = true;
                    }
                    else if(line.startsWith('<style'))
                    {
                            line_is_inside_css_block = true;
                    }
            }
    );
    var output = lines.join('\n');
    output = output.replace(/\n\n+/g,'\n\n');
    output = output.replace(/\<\/p\>\n\<br/g,'\n<br');
    output = output.replace(/\<i\>/g,'<em>');
    output = output.replace(/\<\/i\>/g,'</em>');
    return output;
}

$('button').click(
  function() {
var input_text = $('#input').val();
var output_text = Add_paragraph_tags(input_text);
$('#output').val(output_text);
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<textarea id="input">
This line should have paragraph tags.

<pre>This is code and does not need paragraph tags</pre>

<em>This line should have paragraph tags.</em>

<h1>This is a header and it does not need paragraph tags</h1>

<blockquote>This is a blockquote. This line needs a paragraph tag.

Paragraph tags are needed here too.</blockquote>

<ul>
<li>This is a list item. It does not need a paragraph tag</li>
</ul>

</textarea>

<textarea id="output"></textarea>

<button type="button">Add paragraph tags</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文