2

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.

4

2 回答 2

5

好吧,您可以自己破解(如果我正确理解您的需要)

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

请注意它现在如何缺少所有换行符等...

于 2011-03-09T03:02:17.520 回答
0

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

function Add_paragraph_tags(input_text) {
  var lines = input_text.split("\n");
  line_is_inside_code_block = false;
  line_is_inside_pre_block = false;
  line_is_inside_table = false;
  lines.forEach(function(line, index) {
    if (line == '' || line.startsWith('<h') || line.startsWith('<p') || line.startsWith('<img') || line.startsWith('<pre') || line.startsWith('<code') || line.startsWith('<table') || line.startsWith('<li>') || line.startsWith('<ol>') || line.startsWith('</ol>') || line.startsWith('<ul>') || line.startsWith('</ul>')|| line_is_inside_code_block || line_is_inside_pre_block || line_is_inside_table) {} else {
      lines[index] = '<p>' + line + '</p>';
      lines[index] = lines[index].replace('<p><blockquote>', '<blockquote><p>');
      lines[index] = lines[index].replace('</blockquote></p>', '</p></blockquote>');
    }
    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.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;
    }
  });
  var new_text = lines.join('\n');
  return new_text;
}

$('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>

于 2020-03-22T10:09:35.520 回答