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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:07:20+00:00 2026-05-15T04:07:20+00:00

At the minute I have: $Text = preg_replace(/\[code\](.*?)\[\/code\]/s, <mytag>\\1</mytag>, $Text); How can I escape

  • 0

At the minute I have:

$Text = preg_replace("/\[code\](.*?)\[\/code\]/s", "<mytag>\\1</mytag>", $Text);

How can I escape the backreference using htmlentities()?

  • 1 1 Answer
  • 1 View
  • 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-15T04:07:20+00:00Added an answer on May 15, 2026 at 4:07 am
    $Text = "foo [code]bla<br>bla[/code] foo";
    
    echo preg_replace_callback('/\[code\](.*?)\[\/code\]/s', 'wrap_code', $Text);
    
    function wrap_code($matches) { 
      return '<mytag>'.htmlspecialchars($matches[1]).'</mytag>';
    }
    

    As seen in the preg_replace_callback() documentation, you can also create the callback function inline, via create_function() or as of PHP 5.3.0 even use an anonymous function.


    The generic approach however would be along the lines of this:

    $Text = "foo [code]bla<br>bla[/code] foo";
    
    $re_bbcode = '/\[(code|b|whatever)\](.*?)\[\/\1\]/s';
    echo preg_replace_callback($re_bbcode, 'wrap_code', $Text);
    
    function wrap_code($matches) { 
      switch ($matches[1]) {
        case 'code': $tag = 'mytag'; break;
        case 'b'   : $tag = 'b';     break;
        default    : $tag = ''; 
      }
      if ($tag != '')
        return "<$tag>".htmlspecialchars($matches[2])."</$tag>";
      else
        return htmlspecialchars($matches[0]);
    }
    

    Also I guess (I have not measured) that this regex could be a bit faster, because the reluctant quantifier would not be invoked more often than necessary:

    \[(code|b|whatever)\]([^\[]+|.*?)\[\/\1\]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which loads an image/text into my game.I'm using gluBuild2DMipmaps() to
I have the following code in the head tag of my page: <script type=text/javascript>
I have some code (it's part of a wordpress plugin) which takes a text
In php I have next text: $text='<div id=my_date_div year=2010 month=12 day=07 hour=00 minute=00 >
I have three textboxes in my control,for hour, minute and second. User can input
I have 4 INPUT fields for month, day, hour, minute. <input type=text id=month name=month
I have this if if (hours.Text.Length < 2 || minute.Text.Length < 2) { DialogResult
I have a site where a people can post text. Each post is stored
At the minute I have a page with an AJAX script that searches a
I'm working on a little test application at the minute and I have multiple

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.