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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:01:14+00:00 2026-06-06T18:01:14+00:00

I wish to replace all the link urls in some HTML with a number

  • 0

I wish to replace all the link urls in some HTML with a number sign (#).

The following basically works, however, it to my dismay inserts a <!DOCTYPE>, <html>, and <body> tag around the modified HTML. Is it possible to keep these tags from being inserted? Is there a better way to do this?

Thank you

$html_with_urls = '<p>hello.  Here is a <a href="http://somesite.com">link</a>.  Goodby</p>';
libxml_use_internal_errors(true); //Temorarily disable errors resulting from improperly formed HTML
$doc = new DOMDocument();
$doc->loadHTML($html_with_urls);
$a = $doc->getElementsByTagName('a');
foreach ($a as $link) {
    if ($link->hasAttribute('href')) {
        $link->setAttribute('href', '#');
    }
}
$html_without_urls = $doc->saveHTML();
libxml_use_internal_errors(false);
echo($html_with_urls . '<br />' . $html_without_urls); 
  • 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-06T18:01:17+00:00Added an answer on June 6, 2026 at 6:01 pm

    In my opinion, DOMDocument class has got no option to keep it from adding those extra stuffs. It returns a complete and a valid HTML.

    For your particular case, you could strip those contents off the document yourself:
    [From the code you could tell when it wont work :)]

    $html_without_urls = str_replace(array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $html_without_urls);
    $html_without_urls = preg_replace('/^<!DOCTYPE.+?>/i', '', $html_without_urls);
    echo $html_without_urls;
    

    Or if you could do with some 3rd party libraries, you could use SmartDOMDocumet. Just call the function saveHTMLExact() instead.

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

Sidebar

Related Questions

I wish HTML could do something semantically equivalent to this; <dl class=main-list> <definitionitem> <dt>Some
Given test<-Low-Decarie, Etienne I wish to replace all punctuation with space gsub(pattern=[:punct:], x=test, replacement=
$data contains tabs, leading spaces and multiple spaces. I wish to replace all tabs
I wish to replace all the backslashes (which appear on the same line with
During the table echo how would I replace all digits using regex? I wish
I have a WordPress blog with numerous URL's I wish to replace from this:
I wish to do a search and replace in a string. It searches for
I am writing an XSLT transformation in which I wish to use the Replace
I have a Drupal theme called wellington located in \sites\all\themes\wellington. I wish to override
I have a html string like this: <html><body><p>foo <a href='http://www.example.com'>bar</a> baz</p></body></html> I wish to

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.