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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:27:09+00:00 2026-05-28T17:27:09+00:00

What i tried(it’s a static for cpp only)=> $str1 = <pre class="brush:cpp">; $temp =

  • 0

What i tried(it’s a static for cpp only)=>

$str1 = "<pre                class="brush:cpp">";

$temp =  preg_replace('/&lt;pre\s+class=&quot;brush:cpp&quot;&gt;/','<pre class="brush:cpp">',$str1);

echo $temp . "\n";

That outputs=>

<pre class="brush:cpp">

But the $str1 can be

"&lt;pre class=&quot;brush:cpp&quot;&gt;"
"&lt;pre class=&quot;brush:java&quot;&gt;"
"&lt;pre class=&quot;brush:php&quot;&gt;"
"&lt;pre class=&quot;brush:python&quot;&gt;"

For those the output should be=>

<pre class="brush:cpp">
<pre class="brush:java">
<pre class="brush:php">
<pre class="brush:python">

Note: I can’t use html_entity_decode because the texts will contain other normal string and &lt;br&gt; for <br/>, i don’t want to do html_entity_decode for all texts.

I need a generic regular expression to catch cpp/java/php/python. How can i write a generic regular expression to save that part of pattern and keep it as it is in the replace string.

  • 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-28T17:27:10+00:00Added an answer on May 28, 2026 at 5:27 pm

    I believe something like this will work:

    preg_replace('/&lt;pre\s+class=&quot;brush:(cpp|java|php|python)&quot;&gt;/','<pre class="brush:$1">',$str1);
    

    It uses a capturing group to capture which ending is present, and it can be one of cpp/java/php/python. The replacement is made with the backreference #1, which will place whichever ending was captured.

    Here is an example.

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

Sidebar

Related Questions

Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
tried to research on that but sometimes I seem to lack some googling skills...
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Tried searching in PCRE Documentation . Couldn't find anything. What does & signify in
Tried to rebuild an app that was working just yesterday. Got a message that
Tried to create this Extension method. Works except that the helper is rendering text,
Tried this: Remove-Item C:\foo\.svn but this is the error that I encountered Remove-Item :
Tried running a java method that runs this oracle SQL query String query =
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of

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.