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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:13:01+00:00 2026-05-25T11:13:01+00:00

I have a string value that I’m trying to extract list items for. I’d

  • 0

I have a string value that I’m trying to extract list items for. I’d like to extract the text and any subnodes, however, DOMDocument is converting the entities to the character, instead of leaving in the original state.

I’ve tried setting DOMDocument::resolveExternals and DOMDocument::substituteEntities for false, but this has no effect. It should be noted I’m running on Win7 with PHP 5.2.17.

Example code is:

$example = '<ul><li>text</li>'.
    '<li>&frac12; of this is <strong>strong</strong></li></ul>';

echo 'To be converted:'.PHP_EOL.$example.PHP_EOL;

$doc = new DOMDocument();
$doc->resolveExternals = false;
$doc->substituteEntities = false;

$doc->loadHTML($example);

$domNodeList = $doc->getElementsByTagName('li');
$count = $domNodeList->length;

for ($idx = 0; $idx < $count; $idx++) {
    $value = trim(_get_inner_html($domNodeList->item($idx)));
    /* remainder of processing and storing in database */
    echo 'Saved '.$value.PHP_EOL;
}

function _get_inner_html( $node ) {
    $innerHTML= '';
    $children = $node->childNodes;
    foreach ($children as $child) {
        $innerHTML .= $child->ownerDocument->saveXML( $child );
    }

    return $innerHTML;
}

&frac12; ends up getting converted to ½ (single character / UTF-8 version, not entity version), which is not the desired format.

  • 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-25T11:13:02+00:00Added an answer on May 25, 2026 at 11:13 am

    Based on the answer provided by ajreal, I’ve expanded the example variable to handle more cases, and changed _get_inner_html() to make recursive calls and handle the entity conversion for text nodes.

    It’s probably not the best answer, since it makes some assumptions about the elements (such as no attributes). But since my particular needs don’t require attributes to be carried across (yet.. I’m sure my sample data will throw that one at me later on), this solution works for me.

    $example = '<ul><li>text</li>'.
    '<li>&frac12; of this is <strong>strong</strong></li>'.
    '<li>Entity <strong attr="3">in &frac12; tag</strong></li>'.
    '<li>Nested nodes <strong attr="3">in &frac12; <em>tag &frac12;</em></strong></li>'.
    '</ul>';
    
    echo 'To be converted:'.PHP_EOL.$example.PHP_EOL;
    
    $doc = new DOMDocument();
    $doc->resolveExternals = true;
    $doc->substituteEntities = false;
    
    $doc->loadHTML($example);
    
    $domNodeList = $doc->getElementsByTagName('li');
    $count = $domNodeList->length;
    
    for ($idx = 0; $idx < $count; $idx++) {
        $value = trim(_get_inner_html($domNodeList->item($idx)));
    
        /* remainder of processing and storing in database */
        echo 'Saved '.$value.PHP_EOL;
    
    }
    
    function _get_inner_html( $node ) {
        $innerHTML= '';
        $children = $node->childNodes;
        foreach ($children as $child) {
            echo 'Node type is '.$child->nodeType.PHP_EOL;
            switch ($child->nodeType) {
            case 3:
                $innerHTML .= htmlentities(iconv('UTF-8', 'ISO-8859-1', $child->nodeValue));
                break;
            default:
                echo 'Non text node has '.$child->childNodes->length.' children'.PHP_EOL;
                echo 'Node name '.$child->nodeName.PHP_EOL;
                $innerHTML .= '<'.$child->nodeName.'>';
                $innerHTML .= _get_inner_html( $child );
                $innerHTML .= '</'.$child->nodeName.'>';
                break;
            }
        }
    
        return $innerHTML;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a coded string that I'd like to retrieve a value from. I
I have a string value that I want to write to the registry as
I have a requirement that a user is allowed only to enter string value
I have a class, called DateField , that has a string Value property. If
I have a list of string values that I want add to a hashtable
I have string value in that I need to convert to double in VB.Net.
I have a string value that contains or a Hebrew date, or a Gregorian
I have a string value that needs to be parsed with a regular expression.
Using VB.NET - I have a string: ##RES00012##Some value ##RES00034##Another value That I want
Say I have a string that looks like this: str = The &yquick &cbrown

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.