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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:33:43+00:00 2026-06-17T09:33:43+00:00

Alright, I have some code that will find a <code></code> tag set and clean

  • 0

Alright, I have some code that will find a <code></code> tag set and clean up any code inside of it so it displays instead of functioning like regular code. Everything works, but my problem is how can I find the tag set/multiple tag sets inside, say, $content. Clean the code, and still have ALL of the other content in it? Here is my code, the problem is it checks for matches, and when it finds one it cleans it. But after it cleans it it has no way to put it back into it’s original position $content. ($content is being grabbed from a form)

<?php
preg_match_all("'<code>(.*?)</code>'si", $html, $match);
if ($match) {
  foreach ($match[1] as $snippet) {
    $fixedCode = htmlspecialchars($snippet, ENT_QUOTES);          
  }
}
?>

What do I do with $fixedCode, now that it is clean?

  • 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-17T09:33:43+00:00Added an answer on June 17, 2026 at 9:33 am

    Using regex for parsing HTML is bad. I’d suggest getting familiar with a DOM parser, such as PHP’s DOM module.

    The DOM extension allows you to operate on XML documents through the DOM API with PHP 5.

    Using the DOM module, in order to get the HTML/data from <code> tags in the document, you’d want to do something like this:

    <?php
    //So many variables!
    $html = "<div> Testing some <code>code</code></div><div>Nother div, nother <code>Code</code> tag</div>";
    
    $dom_doc = new DOMDocument;
    
    $dom_doc->loadHTML($html);
    
    $code = $dom_doc->getElementsByTagName('code');
    
    foreach ($code as $scrap) {
        echo htmlspecialchars($scrap->nodeValue, ENT_QUOTES), "<br />";
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, so I have some jQuery code that will send an AJAX request to
So, I have some PHP code that looks a bit like this: <body> The
Alright so I'm having a problem with some code that is generating a null
Alright, I'm copying some code (C++) that needs to run on my server (Python),
Alright, I have a really basic QStandardItemModel , filled with some numbers. I managed
Alright so I have a task, that I have to let the client try
Alright, so I have this collision detection code, and I'm trying to push the
We have some Well-Attributed DB code, like so: [Display(Name = Phone Number)] public string
Alright, I have some weird behavior and this question goes to the people out
Alright, I'm confused as hell. I have an object that I store in a

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.