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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:26:53+00:00 2026-05-23T08:26:53+00:00

I have a string that contains html. How would I htmlentity the string so

  • 0

I have a string that contains html.
How would I htmlentity the string so that everything except tags are encoded?
For example:

$foo = '<div class="link">Here\'s is a link: "<a href="http://www.example.com">Doors & windows</a>'</div>';

I want to convert it to

$out = '<div class="link">Here\'s is a link: &quot;<a href="http://www.example.com">Doors &amp; windows</a>&quot;</div>';
  • 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-23T08:26:54+00:00Added an answer on May 23, 2026 at 8:26 am

    This code snippet shows a function that will load some xml (ensure that at least tags opened have a closing pendant and such, otherwise you will see / read some errors) and then applies htmlentities onto all text-nodes. I actually have no real clue for what you need that, but probably it makes you happy:

    $foo = '<div class="link">Here\'s is a link: <a href="http://www.example.com">Doors & windows</a></div>';
    
    echo text_htmlentities(utf8_encode($foo));
    
    /**
     * add htmlentities onto the text-nodes of an
     * xml fragment.
     * 
     * @param string $foo xml fragment (utf8)
     * @return string
     */
    function text_htmlentities($foo) {
        $foo = str_replace('&', '&amp;', $foo);
        $dom = new DOMDocument;
        $dom->loadXml($foo);
        $xpath = new DomXpath($dom);
        foreach($xpath->query('//text()') as $node) {
            $node->nodeValue = htmlentities($node->nodeValue, ENT_QUOTES, 'UTF-8', false);
        }
        return str_replace('&amp;','&', $dom->saveXml($dom->firstChild));
    }
    

    output:

    <div class="link">Here&#039;s is a link: <a href="http://www.example.com">Doors &amp; windows</a></div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a string that contains the html from a page I just
I have a string of text that contains html, and I need to extract
Suppose I have a string that contains Ü. How would I find all those
I have a String that contains a line of html code eg. '<b>1995</b>' .
I have a string variable that contains the entire HTML of a web page.
I have a string variable that contains the following html data: <p> <em><strong>This is
In Django templates, how would I translate a block that contains HTML? For example:
I have something like: string result = Selenium.GetText(/html/body/form/div[2]); if (result.Contains(test) { bool found =
I have a string variable that contains a lot of HTML markup and I
I have a snippet of HTML that contains paragraphs. (I mean p tags.) I

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.