开发者

PHP Autoformat Paragraph tags

This is a fairly basic question however I've 开发者_Go百科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.


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...


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>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜