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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:16:43+00:00 2026-05-28T02:16:43+00:00

$msg = <body><a>áéíóú☻♥♦♣</a></body>; $temp_dom = new DOMDocument(); $temp_dom->loadHTML($msg); $dom_xpath = new DOMXpath($temp_dom); $ele =

  • 0
$msg = "<body><a>áéíóú☻♥♦♣</a></body>";
$temp_dom = new DOMDocument();    
$temp_dom->loadHTML($msg);    
$dom_xpath = new DOMXpath($temp_dom);   
$ele = $dom_xpath->query('//a')->item(0);

echo "<pre>";
echo "Original: $msg\n";
echo $ele->nodeValue;     
echo "</pre>";

Output:

Original: áéíóú☻♥♦♣
áéíóúâ»â¥â¦â£

The current document encoding is utf-8.
I tried ANSI too and same problem happened.

utf8_decode solves the problem

echo utf8_decode($ele->nodeValue);

But the thing is, I use a lot of attributes and a lot of functions that I would have to use utf8_decode in each one of them, and I believe that’s not the correct thing to do.
Someone know how could I do this?

Please use this test and test it before posting a result, because I’ve already tried a lot of things.

Thank you very much in advance.

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

    The problem is that you need to tell DOMDocument what the encoding is as the HTML is parsed. You can’t do this by setting the encoding option. (I believe that affects how the document is output with saveHTML.)

    The slightly hackish way to do this is to insert a statement of the encoding into the document. You can do this simply by inserting '<?xml encoding="UTF-8">' before the HTML you are parsing.

    <?php
    
    $msg = "<body><a>áéíóú☻♥♦♣</a></body>";
    $temp_dom = new DOMDocument();    
    
    
    $temp_dom->loadHTML('<?xml encoding="UTF-8">' . $msg);    
    $temp_dom->encoding = 'UTF-8';
    $dom_xpath = new DOMXpath($temp_dom);   
    $ele = $dom_xpath->query('//a')->item(0);
    
    echo "<pre>";
    echo "Original: $msg\n";
    echo $ele->nodeValue;     
    echo "</pre>";
    

    Output:

    <pre>Original: <body><a>áéíóú☻♥♦♣</a></body>
    áéíóú☻♥♦♣</pre>
    

    Note, however, that this does insert an extra node as a child of the document object (a DOMProcessingInstruction to be precise), so be aware of this if you are doing anything with $temp_dom->childNodes or suchlike.

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

Sidebar

Related Questions

<body> <div> <?= $_POST['msg'] ?> </div> <form id=frm method=post> <input type=hidden name='msg' value='{field0: Im
I'm using the following basic code: System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.to.add(someone@hotmail.com); msg.to.add(someone@gmail.com); msg.to.add(someone@myDomain.com);
This my code that i wrote it MailMessage msg = new MailMessage(); msg.From =
Consider this code private MailMessage GetMailMessageFromMailItem(Data.SystemX.MailItem mailItem) { var msg = new MailMessage(); foreach
Msg 701, Level 17, State 123, Line 1 There is insufficient system memory to
NSString *msg = [[NSString alloc]initWithFormat:@Hello %s, What do you do, (self.isUser ? @User :
if($err) $_SESSION['msg']['login-err'] = implode('<br />',$err); Three quick questions: Why use a two-dimensional array here?
syntax error: msg = keyword can't be an expression offset = None print_file_and_line =
fprintf(pFile,msg.c_str()); why do I get a warning in Xcode : Format string is not
void say(char msg[]) { // using pointer to print out the first char 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.