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 6336195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:00:19+00:00 2026-05-24T19:00:19+00:00

Currenlty I’m creating a bbCodes function to replace all bbCodes by their corresponding HTML

  • 0

Currenlty I’m creating a bbCodes function to replace all bbCodes by their corresponding HTML codes within a text. My code works like this atm:

public function bbCodes($text) {

    global $bb_codes;

    $text = preg_replace(array_keys($bb_codes), array_values($bb_codes), $text);

    return $text;

}

where $bb_codes looks like this:

$bb_codes = array(
            "/\[b\](.*)\[\/b\]/is" => "<b>$1</b>",
            "/\[u\](.*)\[\/u\]/" => "<u>$1</u>",
            "/\[i\](.*)\[\/u\]/" => "<i>$1</i>",
            "/\[d\](.*)\[\/d\]/" => "<del>$1</del>",
            "/\[url=(.*)\](.*)\[\/url\]/" => "<a href='$1'>$2</a>"
              );

It’s working when each bbcode is only used once, e.g.:

[b]this text is bold[/b]
[i]this text is italic[/i]
etc..

But as soon as I use one bbcode tag multiple times it gets messed up:

   [b]this text is bold[/b]
   [i]this text is italic[/i]
   [b]this text is bold too[/b]

It will see the first [b] tag and look for another [/b] tag but it takes the last one instead of the first one it encounters (in the above example all the text will be bold and the first [b] and last [/b] will be replaced only). Does anyone know what I’ve done wrong and how I can fix this?

Thanks in advanced!

Cheers.

  • 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-05-24T19:00:22+00:00Added an answer on May 24, 2026 at 7:00 pm

    You need to use non greedy wildcard, instead of all the .* write .*?.

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

Sidebar

Related Questions

I have an XML file that needs parsing in PHP. I am currenlty using
Currently I using the following code to get my JSON output from MySQL. <?php
Currently, my code shows a loading spinner gif, returns the data and caches it.
Currently I have some legacy ASP.NET 2.0 code that uses the ASP Xml web
I'm currenlty putting together an If, ElseIf and Else statement to go through the
Currently I am doing one query, with 3 sub queries, all queries are on
Currenlty I am trying to publish usinf aspnet compiler using a batch file start
Hey guys I'm currenlty working on something and I want if I click a
I'm currenlty using SSIS for some process flow, scripting, and straight data import. Most
I am currenlty using the JTable rowFilter for filtering my rows. The filter hide

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.