Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8066641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:04:31+00:00 2026-06-05T12:04:31+00:00

I am stuck working on a function that translates HTML to bbcode. I have

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:04:31+00:00Added an answer on June 5, 2026 at 12:04 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a problem and got stuck at a wall I have
I'm working through the rails tutorial and have gotten stuck. Starting at Listing 8.16
I'm working through the Grails in Action book, and I'm stuck at that part
I have developed a Wordpress site that loads pages dynamically using the .load function.
I am working on an image manipulation problem. I have an overhead projector that
I'm working on a Function that will do a POW long handed. It's an
I'm working with xslt for the first time. I have 2.0, but that's about
I'm working on some map stuff and I'm stuck with something that seems to
Im stuck with a ajax function im creating. Here is the deal, I have
I have this function in vb.net that I converted from C# for a project

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.