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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:44:12+00:00 2026-06-18T20:44:12+00:00

I am searching for script which remove all attributes except href in anchor tag.

  • 0

I am searching for script which remove all attributes except href in anchor tag. I found some scripts which all very similar following solution i write.

$okattrs = array(
            'a' => array('href' => true),
            );

$content = '<div id="att"><a class="pl" title="hit me" id="myid" href="http://google.com" >Click On Google</a></div>';
$doc = new DomDocument();
$doc->loadHTML($content);

$a_tags = $doc->getElementsByTagName('a');
foreach($a_tags as $k => $a_tag_obj) {
    foreach ($a_tag_obj->attributes as $name => $attrNode)
    {
        if (!isset($okattrs[$a_tag_obj->nodeName][$name]))
        {
            $a_tag_obj->removeAttributeNode($attrNode);
        }
    }
}
echo    $content1=$doc->saveHTML();

Now my problem is that it remove only first attribute which is ‘class’. I debug and found if i remove line having removeAttributeNode loop works fine 4 times, But in case of using this loop just work one time, remove only first attribute and breaks.

Anybody have this problem before or have a solution?

  • 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-18T20:44:14+00:00Added an answer on June 18, 2026 at 8:44 pm

    Finally i make the code running. just one change that move removeAttributeNode to another loop.

    foreach($a_tags as $a_tag_obj) { 
        $attribute_list = array(); 
        foreach ($a_tag_obj->attributes as $name => $attrNode) { 
            if (!isset($okattrs[$a_tag_obj->nodeName][$name])) { 
                $attribute_list[] = $name; 
            }    
        } 
        for($i=0;$i<sizeof($attribute_list);$i++) { 
            $a_tag_obj->removeAttribute($attribute_list[$i]); 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Searching for a js script, which will show some message (something like Loading, please
I've done a bit of searching on this. Found plenty of scripts that counted
I need to create a script searching for words with 'blanks', which basically are
I'm searching for a jQuery or simple, old-school JavaScript script which makes text input
I am writing a basic script which just extracts all the links from a
after searching google i got a code which works but this code download all
I have a powershell script which outputs all Exchange 2003 mailboxes by size. $computers
I am new to searching in MySQL and I have some tasks, which I
I have the following PHP script which runs nicely and inserts all the data
I was searching for a drop down list script that auto-updates the content below

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.