自动< p>正则表达式-需要修复 [英] Auto <p> regex - fix needed

查看:54
本文介绍了自动< p>正则表达式-需要修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能(我在Stackoverflow的某个地方找到了),可以在输出字符串中自动添加< p> 标记.

I have this function (which I found somewhere in Stackoverflow) to automatically add <p> tags in a string for the output.

function autop ($string) {

    // Define block tags
    $block_tag_list = array ('address', 'applet', 'article', 'aside', 'audio', 'blockquote', 'button', 'canvas', 'center', 'command', 'data', 'datalist', 'dd', 'del', 'details', 'dir', 'div', 'dl', 'dt', 'embed', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'iframe', 'ins', 'isindex', 'li', 'map', 'menu', 'nav', 'noframes', 'noscript', 'object', 'ol', 'output', 'p', 'pre', 'progress', 'section', 'script', 'summary', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'time', 'tr', 'track', 'ul', 'video');

    $tags = '<' . implode ('[^>]*>|<', $block_tag_list) . '[^>]*>';

$pattern = <<<PATTERN
/
(\A|\\n\\n)(?!$tags) # Start of string or two linebreaks or anything but a block tag
(.+?) # Just about anything
(\Z|\\n\\n) # End of string or two line breaks
/isex
PATTERN;

    $string = str_replace ("\r\n", "\n", $string);
    $string = str_replace ("\r\t", "", $string);
    $string = str_replace ("\n\t", "", $string);
    $string = str_replace ("\t", "", $string);
    $string = preg_replace ($pattern, "'\\1<p>' . nl2br ('\\2') . '</p>\\3'", $string);
    $string = preg_replace ($pattern, "'\\1<p>' . nl2br ('\\2') . '</p>\\3'", $string);
    $string = str_replace ('\"', "&quot;", $string);

    return $string;
}

具有这种类型的字符串:

Having this type of string:

<h1>Title</h1>

This will be wrapped in a p tag

This should be wrapped in a p tag too

它输出

<h1>Title</h1>

<p>This will be wrapped in a p tag</p>

<p>This should be wrapped in a p tag too</p>

它可以正常工作,但是有一个问题:它将HTML标记包装在其他&p; p> 标记之后的< p> 标记之后,从而将代码.如果HTML标记位于< h1> 或其他任何块标记之后,则不会发生这种情况.

It works fine, but for one problem: it wraps HTML tags which are immediately after a <p> tag in other <p> tags, screwing the code. It does not happen if the HTML tags are after a <h1> or whatever other block tag.

将双 preg_replace 设为单个即可解决该问题,但是,如果像前面的示例中那样有两个段落,它将仅包装第一个而不是第二个.

Making the double preg_replace a single one solves the problem, but then if there are two paragraphs like in the example before, it only wraps the first one and not the second.

我觉得这只是一个很小的变化,可能会使它变滴答",但我无法弄清楚.

I feel it's only a small change which could just make it "tick", but I can't figure it out.

也许有人打了天才...:)

Maybe if someone had a strike of genius... :)

推荐答案

我不确定您是否会对您的解决方案感到满意,但是您应该尝试执行此操作(观看添加的<第五行中的code>?= ):

I am not sure if you will be happy with your solution all the way, but you should get what you are trying to do with this (watch the added ?= in the 5th line):

$pattern = <<<PATTERN
/
(\A|\\n\\n)(?!$tags) # Start of string or two linebreaks or anything but a block tag
(.+?) # Just about anything
(?=\Z|\\n\\n) # End of string or two line breaks
/isex
PATTERN;

否则,前一个边界 \ Z 将消耗下一个 \ A ,因此不再匹配.当然,请删除双 preg_replace .

Without this the previous boundary \Z would consume the next \A and therefore this would not match anymore. And of course remove the double preg_replace.

希望这会有所帮助.

这篇关于自动&lt; p&gt;正则表达式-需要修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆