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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:12:32+00:00 2026-06-13T13:12:32+00:00

I have this preg_replace patterns and replacements : $patterns = array( /<br\W*?\/>/, /<strong>/, /<*\/strong>/,

  • 0

I have this preg_replace patterns and replacements :

$patterns = array(
    "/<br\W*?\/>/",
    "/<strong>/",
    "/<*\/strong>/",
    "/<h1>/",
    "/<*\/h1>/",
    "/<h2>/",
    "/<*\/h2>/",
    "/<em>/",
    "/<*\/em>/",
    '/(?:\<code*\>([^\<]*)\<\/code\>)/',
);
$replacements = array(
    "\n",
    "[b]",
    "[/b]",
    "[h1]",
    "[/h1]",
    "[h2]",
    "[/h2]",
    "[i]",
    "[/i]",
    '[code]***HTML DECODE HERE***[/code]',
);

In my string I want to html_entity_decode the content between these tags :
<code> &lt; $gt; </code> but keep my array structure for preg replace

so this : <code> &lt; &gt; </code> will be this : [code] < > [/code]

Any help will be very appreciated, thanks!

  • 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-13T13:12:33+00:00Added an answer on June 13, 2026 at 1:12 pm

    You cannot encode this in the replacement string. As PoloRM suggested, you could use preg_replace_callback specifically for your last replacement instead:

    function decode_html($matches)
    {
        return '[code]'.html_entity_decode($matches[1]).'[/code]';
    }
    
    $str = '<code> &lt; &gt; </code>';
    $str = preg_replace_callback('/(?:\<code*\>([^\<]*)\<\/code\>)/', 'decode_html', $str);
    

    Equivalently, using create_function:

    $str = preg_replace_callback(
        '/(?:\<code*\>([^\<]*)\<\/code\>)/',
        create_function(
           '$matches',
            'return \'[code]\'.html_entity_decode($matches[1]).\'[/code]\';'
        ),
        $str
    );
    

    Or, as of PHP 5.3.0:

    $str = preg_replace_callback(
        '/(?:\<code*\>([^\<]*)\<\/code\>)/',
        function ($matches) {
            return '[code]'.html_entity_decode($matches[1]).'[/code]';
        },
        $str
    );
    

    But note that in all three cases, your pattern is not really optimal. Firstly, you don’t need to escape those < and > (but that is just for readability). Secondly, your first * allows infinite repetition (or omission) of the letter e. I suppose you wanted to allow attributes. Thirdly, you cannot include other tags within your <code> (because [^<] will not match them). In this case maybe you should go with ungreedy repetition instead (I also changed the delimiter for convenience):

    ~(?:<code[^>]*>(.*?)</code>)~
    

    As you can already see, this is still far from perfect (in terms of correctly matching the HTML in the first place). Hence, the obligatory reminder: don’t use regex to parse HTML. You will be much better off, using a DOM parser. PHP brings a built-in one, and there is also this very convenient-to-use 3rd-party one.

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

Sidebar

Related Questions

we have this code: $value = preg_replace(/[^\w]/, '', $value); where $value is in utf-8.
currently, this is the preg_replace string that I have. $mtg_post['post_content'] = preg_replace([^\\x20-\\x7e], ,$ability); This
I have this string 2012-06-27 16:17:06 and I want to convert it to GMT
My preg_replace pattern regex code here.. /<img(.*?)src=(.*?)/ This is my replace code.. <img$1src='.$path.'$2 So
So this is a preg_replace associated question i guess, I have a string with
I have a String like this {\i1}You were happy?{\i0} and I want to remove
I have this html code <span><style> .p9Wu{display:none} .JOz4{display:inline} .nitJ{display:none} .pIj0{display:inline} .AU2Q{display:none} .vDdS{display:inline} .THa1{display:none} .mRy1{display:inline}
I have this string: 4702819 - 09 (without the quotes) and i want to
I want to do the following: I have a string in a database, it
Alright, this problem seems to be way above my head! I have this code:

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.