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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:17:25+00:00 2026-05-23T02:17:25+00:00

I’m trying to run the preg_replace() function to replace the content in between two

  • 0

I’m trying to run the preg_replace() function to replace the content in between two custom tags (i.e. [xcode]) within the string / content of the page.

What I want to do with the content between these custom tags is to run it through highlight_string() function and to remove those custom tags from the output.

Any idea how to do it?

  • 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-23T02:17:25+00:00Added an answer on May 23, 2026 at 2:17 am

    So you want sort of a BBCode parser. The example below replaces [xcode] tags with whatever markup you like.

    <?php
    function highlight($text) {
        $text = preg_replace('#\[xcode\](.+?)\[\/xcode\]#msi', '<em>\1</em>', $text);
        return $text;
    }
    $text = '[xcode]Lorem ipsum[/xcode] dolor sit [xcode]amet[/xcode].';
    echo highlight($text);
    ?>
    

    Use preg_replace_callback() if you want to pass the matched text to a function:

    <?php
    function parse($text) {
        $text = preg_replace_callback('#\[xcode\](.+?)\[\/xcode\]#msi',
            function($matches) {
                return highlight_string($matches[1], 1);
            }
        , $text);
        return $text;
    }
    $text = '[xcode]Lorem ipsum[/xcode] dolor sit [xcode]amet[/xcode].';
    echo bbcode($text);
    ?>
    

    I’ll include the source code of a BBCode parser that I made a long time ago. Feel free to use it.

    <?php
    function bbcode_lists($text) {
        $pattern = "#\[list(\=(1|a))?\](.*?)\[\/list\]#msi";
        while (preg_match($pattern, $text, $matches)) {
            $points = explode("[*]", $matches[3]);
            array_shift($points);
            for ($i = 0; $i < count($points); $i++) {
                $nls = split("[\n]", $points[$i]);
                $brs = count($nls) - 2;
                $points[$i] = preg_replace("[\r\n]", "<br />", $points[$i], $brs);
            }
            $replace = ($matches[2] != '1') ? ($matches[2] != 'a') ? '<ul>' : '<ol style="list-style:lower-alpha">' : '<ol style="list-style:decimal">';
            $replace .= "<li>";
            $replace .= implode("</li><li>", $points);
            $replace .= "</li>";
            $replace .= ($matches[2] == '1' || $matches[2] == 'a' ) ? '</ol>' : '</ul>';
            $text = preg_replace($pattern, $replace, $text, 1);
            $text = preg_replace("[\r\n]", "", $text);
        }
        return $text;
    }
    function bbcode_parse($text) {
        $text = preg_replace("[\r\n]", "<br />", $text);
        $smilies = Array(
            ':)' => 'smile.gif',
            ':d' => 'tongue2.gif',
            ':P' => 'tongue.gif',
            ':lol:' => 'lol.gif',
            ':D' => 'biggrin.gif',
            ';)' => 'wink.gif',
            ':zzz:' => 'zzz.gif',
            ':confused:' => 'confused.gif'
        );
        foreach ($smilies as $key => $value) {
            $text = str_replace($key, '<img src="/images/smilies/' . $value . '" alt="' . $key . '" />', $text);
        }
        if (!(!strpos($text, "[") && !strpos($text, "]"))) {
            $bbcodes = Array(
                '#\[b\](.*?)\[/b\]#si' => '<strong>$1</strong>',
                '#\[i\](.*?)\[/i\]#si' => '<em>$1</em>',
                '#\[u\](.*?)\[/u\]#si' => '<span class="u">$1</span>',
                '#\[s\](.*?)\[/s\]#si' => '<span class="s">$1</span>',
                '#\[size=(.*?)\](.*?)\[/size\]#si' => '<span style="font-size:$1">$2</span>',
                '#\[color=(.*?)\](.*?)\[/color\]#si' => '<span style="color:$1">$2</span>',
                '#\[url=(.*?)\](.*?)\[/url\]#si' => '<a href="$1" target="_blank">$2</a>',
                '#\[url\](.*?)\[/url\]#si' => '<a href="$1" target="_blank">$1</a>',
                '#\[img\](.*?)\[/img\]#si' => '<img src="$1" alt="" />',
                '#\[code\](.*?)\[/code\]#si' => '<div class="code">$1</div>'
            );
            $text = preg_replace(array_keys($bbcodes), $bbcodes, $text);
    
            $text = bbcode_lists($text);
    
            $quote_code = Array("'\[quote=(.*?)\](.*?)'i", "'\[quote](.*?)'i", "'\[/quote\]'i");
            $quote_html = Array('<blockquote><p class="quotetitle">Quote \1:</p>\2', '<blockquote>\2', '</blockquote>');
            $text = preg_replace($quote_code, $quote_html, $text);
    
        }
        return $text;
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Specifically, suppose I start with the string string =hello \'i am \' me And

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.