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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:13:56+00:00 2026-06-02T16:13:56+00:00

<?php function walkDOM($node) { if (! isset($node->childNodes)) return; for ($i = 0; $i <

  • 0
<?php
function walkDOM($node)
{
    if (! isset($node->childNodes))
        return;

    for ($i = 0; $i < $node->childNodes->length; $i++) {

        $childNode = $node->childNodes->item($i);
        $childNodeName = $childNode->nodeName;

        echo $childNode->nodeName . " - " . $childNode->nodeType . 
             " - \"" . $childNode->nodeValue . "\"\n";
        walkDOM($childNode);
    }
}

function processHTML($s)
{
    $doc = new DOMDocument('1.0', 'UTF-8');
    $success = $doc->loadHTML($s);
    if (! $success) {
        echo "Load HTML failed.\n";
        exit(1);
    }
    echo "Loaded HTML: " . $doc->saveHTML() . "\n";
    walkDOM($doc);
}

$s = '<div>hello, <p>world<big>!</big></p></div>';
processHTML($s);
?>

Output:

Loaded HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div>hello, <p>world<big>!</big></p></div></body></html>

html - 10 - ""
html - 1 - "hello, world!"
body - 1 - "hello, world!"
div - 1 - "hello, world!"
#text - 3 - "hello, "
p - 1 - "world!"
#text - 3 - "world"
big - 1 - "!"
#text - 3 - "!"

From the above code and output, we can see that when we access nodeValue property of any DOMNode we get its inner HTML with all tags stripped off. I can use this to filter out all tags as follows:

$s = '<div>hello, <p>world<big>!</big></p></div>';
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadHTML($s);
echo $doc->childNodes->item(1)->nodeValue . "\n";

Output:

hello, world!

But I can do so using strip_tags as well:

$s = '<div>hello, <p>world<big>!</big></p></div>';
echo strip_tags($s) . "\n";

I have two questions:

  1. Can I rely on this behavior of nodeValue property in future to strip tags or do any other kind of thing that I can imagine. Are there any hidden surprises?
  2. How is using nodeValue to strip tags different from using strip_tags() to strip tags?
  • 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-02T16:13:56+00:00Added an answer on June 2, 2026 at 4:13 pm

    If you just want to strip tags, whay complicate? I think it is faster, and better to use the strip_tags() function. Also, it can take a second parameter to specify tags which should not be stripped.

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

Sidebar

Related Questions

I have created a php function to return time in digital clock format. My
I need a php function that will remove and return any references to an
The php function sprintf does not return the exact value; The sample code is
This PHP function working just fine: if (preg_match (/<(\S+@\S+\w)>.*\n?.*55[0-9]/i,$body,$match)) { echo found!; } in
I have this PHP function : if(($_POST['id']==pm_read) && (isset($_POST['pm_id'])) && (ctype_digit($_POST['pm_id'])) && (isset($_SESSION['nickname']))) {
In this php function I append to the node by calling that function in
Is the a proper php function to echo javascript string? I want to the
Can the php function mysql_insert_id() return no result after processing the INSERT query in
I need a php function that can randomly select one row that has a
I have defined a php function: function my_function($text = '', $relative = false, $icon

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.