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

  • Home
  • SEARCH
  • 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 7770193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:20:56+00:00 2026-06-01T16:20:56+00:00

So I have created this function in PHP to output text in the required

  • 0

So I have created this function in PHP to output text in the required form. It is a simple BB-Code system. I have cut out the other BB-Codes from it to keep it shorter (Around 15 cut out)

My issue is the final one [title=blue]Test[/title] (Test data) does not work. It outputs exactly the same. I have tried 4-5 different versions of the REGEX code and nothing has changed it.

Does anyone know where I am going wrong or how to fix it?

function bbcode_format($str){
$str = htmlentities($str);
$format_search =  array(
'#\[b\](.*?)\[/b\]#is',
'#\[title=(.*?)\](.*?)\[/title\]#i'
);
$format_replace = array(
'<strong>$1</strong>',
'<div class="box_header" id="$1"><center>$2</center></div>'
);
$str = preg_replace($format_search, $format_replace, $str);
$str = nl2br($str);
return $str;
}
  • 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-01T16:20:57+00:00Added an answer on June 1, 2026 at 4:20 pm

    Change the delimiter # to /. And change “/[/b\]” to “\[\/b\]“. You need to escape the “/” since you need it as literal character.

    Maybe the “array()” should use brackets: “array[]“.

    Note: I borrowed the answer from here: Convert BBcode to HTML using JavaScript/jQuery

    Edit: I forgot that “/” isn’t a metacharacter so I edited the answer accordingly.

    Update: I wasn’t able to make it work with function, but this one works. See the comments. (I used the fiddle on the accepted answer for testing from the question I linked above. You may do so also.) Please note that this is JavaScript. You had PHP code in your question. (I can’t help you with PHP code at least for awhile.)

    $str = 'this is a [b]bolded[/b], [title=xyz xyz]Title of something[/title]';
    
    //doesn't work (PHP function)
    //$str = htmlentities($str);
    
    //notes: lose the single quotes
    //lose the text "array" and use brackets
    //don't know what "ig" means but doesn't work without them
    $format_search =  [
    /\[b\](.*?)\[\/b\]/ig,
    /\[title=(.*?)\](.*?)\[\/title\]/ig
    ];
    
    $format_replace = [
      '<strong>$1</strong>',
      '<div class="box_header" id="$1"><center>$2</center></div>'
    ];
    
    // Perform the actual conversion
    for (var i =0;i<$format_search.length;i++) {
      $str = $str.replace($format_search[i], $format_replace[i]);
    }
    
    //place the formatted string somewhere
    document.getElementById('output_area').innerHTML=$str;
    

    ​

    Update2: Now with PHP… (Sorry, you have to format the $replacements to your liking. I just added some tags and text to demostrate the changes.) If there’s still trouble with the “title”, see what kind of text you are trying to format. I made the title “=” optional with ? so it should work properly work texts like: “[title=id with one or more words]Title with id[/title]” and “[title]Title without id[/title]. Not sure thought if the id attribute is allowed to have spaces, I guess not: http://reference.sitepoint.com/html/core-attributes/id.

    $str = '[title=title id]Title text[/title] No style, [b]Bold[/b], [i]emphasis[/i], no style.';
    
    //try without this if there's trouble
    $str = htmlentities($str);
    
    //"#" works as delimiter in PHP (not sure abut JS) so no need to escape the "/" with a "\"
    $patterns = array();
    $patterns = array(
      '#\[b\](.*?)\[/b\]#',
      '#\[i\](.*?)\[/i\]#', //delete this row if you don't neet emphasis style
      '#\[title=?(.*?)\](.*?)\[/title\]#'
    );
    
    $replacements = array();
    $replacements = array(
      '<strong>$1</strong>',
      '<em>$1</em>', // delete this row if you don't need emphasis style
      '<h1 id="$1">$2</h1>'
    );
    
    //perform the conversion
    $str = preg_replace($patterns, $replacements, $str);
    echo $str;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have created a module with this among others this function in it: <?php
I have this problem whereby, i have created a user defined function within php.
I have a PHP function that returns an array. This is the output of
I have dialog created like this $('#add_error').click(function(e) { $('<div>') .load('/someaction/format/html/') .dialog({ title: 'Some title',
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });
I have this class that have a function to load other classes and create
I have created this code, and when I run it, don't get any errors
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have created a php person class and a very basic form. The form
In PHP, I have created a user defined function. Example: <?php function test($one, $two)

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.