PHP Autoformat Paragraph标签

时间:2011-03-09 02:42:08

标签: php html autoformatting

这是一个相当基本的问题,但我只能找到三个答案。我想autoparagraph无格式内容,可能包含或不包含其他html元素。这两个好的功能是wordpress wpautopHTMLPurifier AutoFormat.AutoParagraph,最后是一些随机addParagraphsNew。但是第一个与我的应用程序许可证不兼容,第二个不在段落标记中添加换行符,第三个是flakey。

是否有人知道商业上合适的许可脚本允许将包含换行符和双换行符的html内容转换为<br /><p>标记?

我相信HTMLPurifier选项可能是我攻击AutoFormat.AutoParagraph插件以使其添加换行符的最佳方式,但我希望稍微容易一些。懒惰,我知道。

2 个答案:

答案 0 :(得分:3)

你可以自己破解它(如果我理解你需要的话)

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

注意它现在缺少所有新行等...

答案 1 :(得分: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>