douhan4243 2012-06-10 16:13
浏览 27
已采纳

PHP:相同的字符串没有匹配在一起,与之相关(

I am stuck working on a function that translates HTML to bbcode. I have written my own [spoiler] bbcode tag which translates properly into the HTML equivalent.

But when I try to turn it back into bbcode it doesn't seem to match seemingly identical strings...

After slowly rebuilding it piece by piece to see where the problem is, it turns out that it only fails when I add onclick="showSpoiler(this)"

to

#<div><input type="button" onclick="showSpoiler(this)"/><div>(.*)</div></div>#ig'

I narrowed it further down to the ( brackets. I have tried to escape them like this \(

the html code that is generated from the [spoiler] tag is:

`$1

and the string that it is matched against is this

'#<div><input type="button" onclick="showSpoiler(this)"/><div>(.*)</div></div>#ig'

here are the conversion functions

<?php
//This function let convert BBcode to HTML
function bbcode_to_html($text)
{
    $text = nl2br(htmlentities($text, ENT_QUOTES, 'UTF-8'));
    $in = array(
            '#\[b\](.*)\[/b\]#Usi',
            '#\[i\](.*)\[/i\]#Usi',
            '#\[u\](.*)\[/u\]#Usi',
            '#\[s\](.*)\[/s\]#Usi',
            '#\[img\](.*)\[/img\]#Usi',
            '#\[url\]((ht|f)tps?\:\/\/(.*))\[/url\]#Usi',
            '#\[url=((ht|f)tps?\:\/\/(.*))\](.*)\[/url\]#Usi',
            '#\[left\](.*)\[/left\]#Usi',
            '#\[center\](.*)\[/center\]#Usi',
            '#\[right\](.*)\[/right\]#Usi',
            '#\[spoiler\](.*)\[/spoiler\]#Usi',
            '#\[fuck\](.*)\[/fuck\]#Usi'
        );
    $out = array(
            '<strong>$1</strong>',
            '<em>$1</em>',
            '<span style="text-decoration:underline;">$1</span>',
            '<span style="text-decoration:line-through;">$1</span>',
            '<img src="$1" alt="Image" />',
            '<a href="$1">$1</a>',
            '<a href="$1">$4</a>',
            '<div style="text-align:left;">$1</div>',
            '<div style="text-align:center;">$1</div>',
            '<div style="text-align:right;">$1</div>',
            '<div><input type="button" onclick="showSpoiler(this)" value="Show/Hide" /><div class="inner" style="display:none;">$1</div></div>',
            '<div><input type="button" onclick="showSpoiler(this)"/><div>$1</div></div>'
        );
    $count = count($in)-1;
    for($i=0;$i<=$count;$i++)
    {
        $text = preg_replace($in[$i],$out[$i],$text);
    }
    return $text;
}
//This function let convert HTML to BBcode
function html_to_bbcode($text)
{
    $text = str_replace('<br />','',$text);
    $in = array(
        '#<strong>(.*)</strong>#Usi',
        '#<em>(.*)</em>#Usi',
        '#<span style="text-decoration:underline;">(.*)</span>#Usi',
        '#<span style="text-decoration:line-through;">(.*)</span>#Usi',
        '#<img src="(.*)" alt="Image" />#Usi',
        '#<a href="(.*)">(.*)</a>#Usi',
        '#<div style="text-align:left;">(.*)</div>#Usi',
        '#<div style="text-align:center;">(.*)</div>#Usi',
        '#<div style="text-align:right;">(.*)</div>#Usi',
        '#<div><input type="button" onclick="showSpoiler(this)" value="Show/Hide" /><div class="inner" style="display:none;">(.*)</div></div>#Ui',
        '#<div><input type="button" onclick="showSpoiler(this)"/><div>(.*)</div></div>#ig'
    );
    $out = array(
        '[b]$1[/b]',
        '[i]$1[/i]',
        '[u]$1[/u]',
        '[s]$1[/s]',
        '[img]$1[/img]',
        '[url=$1]$2[/url]',
        '[left]$1[/left]',
        '[center]$1[/center]',
        '[right]$1[/right]',
        '[spoiler]$1[/spoiler]',
        '[fuck]$1[/fuck]'
    );
    $count = count($in)-1;
    for($i=0;$i<=$count;$i++)
    {
        $text = preg_replace($in[$i],$out[$i],$text);
    }
    return $text;
}
?>
  • 写回答

1条回答 默认 最新

  • doutinghou6980 2012-06-10 17:01
    关注

    In your regex you need to escape the braces like so:

    showSpoiler\(this\)
    

    Take care with regular expressions, they are a language on it's own and hard to debug unless you add more functions that do the debugging (e.g. what was matched, output that etc.).

    BTW you can run multiple search and replace operations by directly passing the arrays into the function. You don't need to iterate over them.

    So better read the manual page about preg_replace again and look forward how you can more easily debug your patterns. E.g. test them before putting them into the function and similar.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀