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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:00:10+00:00 2026-05-20T02:00:10+00:00

I want to run htmlentities() only on the contents within <code> things to strip

  • 0

I want to run htmlentities() only on the contents within <code> things to strip </code>

I Wrote a function that takes a string and finds the content in between <code> </code>


function parse_code($string) {

        // test the string and find contents with <code></code>
        preg_match('@<code>(.*?)</code>@s', $string, $matches);

            // return the match if it succeeded
            if($matches) {
                return $matches[1];
            }else {
                return false;
            }
    }

However I need some help with a function that will will actually run htmlentities(); on the contents within <code> </code> and then implode() it all back together. For example lets say we have the string below.

<div class="myclass" rel="stuff"> things in here </div>
<code> only run htmlentites() here so strip out things like < > " ' & </code>
<div> things in here </div>

Once again, The function would need to keep all contents of the string the same but only modify and run htmlentities() on the contents of <code> </code>

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

    You can simplify this with using a custom callback function:

    $html = preg_replace_callback(
         "@  (?<= <code>)  .*?  (?= </code>)  @six",
         "htmlentities_cb", $html
    );
    
    function htmlentities_cb($matches) {
        return htmlentities($matches[0], ENT_QUOTES, "UTF-8");
    }
    

    The syntax for matching the enclosing code tags is called lookbehind and lookahead assertion. It simplifies the callback and avoids implode() later, because the assertion matches itself do not become part of $matches[0]. While @six is for case-insensitve tag matches and allows whitespace in the regex to make it more readable.

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

Sidebar

Related Questions

I want to run this function, or at least the bit that deletes the
I want to run a PHP file/function that will not stop until it finishes
I am writing an NUnit test that I want run only in the Release
I want to run javascript/Python/Ruby inside my application. I have an application that creates
I want to run a background task that reads input from a TextReader and
I want to run an Apache on my local machine since I can only
I want to run a console application that will output a file. I user
I run a website (sorta like a social network) that I wrote myself. I
Why this code don't work,when i want run this code vwd 2008 express show
I want to run some code when an app is launched, so my broadcast

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.