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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:41:48+00:00 2026-05-16T23:41:48+00:00

Note: The input HTML is trusted; it is not user defined! I’ll highlight what

  • 0

Note: The input HTML is trusted; it is not user defined!

I’ll highlight what I need with an example.

Given the following HTML:

<p>
  Welcome to <a href="http://google.com/" class="crap">Google.com</a>!<br>
  Please, <a href="enjoy.html">enjoy</a> your stay!
</p>

I’d like to to convert it to:

Welcome to Google.com[1]
Please, enjoy[2] your stay!

[1] http://google.com/
[2] %request-uri%/enjoy.html    <- note, request uri is something I define
                                   for relative paths

I’d like to be able to customize it.


Edit: On a further note, I’d better explain myself and my reasons

We have an automated templating system (with sylesheets!) for emails and as part of the system, I’d like to generate multipart emails, ie, which contain both HTML and TEXT.
The system is made to only provides HTML.

I need to convert this HTML to text meaningfully, eg, I’d like to somehow retain any links and images, perhaps in the format I specified above.

  • 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-16T23:41:49+00:00Added an answer on May 16, 2026 at 11:41 pm

    You could use the DOM to do the following:

    $doc = new DOMDocument();
    $doc->loadHTML('…');
    
    $anchors = array();
    foreach ($doc->getElementsByTagName('a') as $anchor) {
        if ($anchor->hasAttribute('href')) {
            $href = $anchor->getAttribute('href');
            if (!isset($anchors[$href])) {
                $anchors[$href] = count($anchors) + 1;
            }
            $index = $anchors[$href];
            $anchor->parentNode->replaceChild($doc->createElement('a', $anchor->nodeValue." [$index]"), $anchor);
        }
    }
    $html = strip_tags($doc->saveHTML());
    $html = preg_replace('/^[\t ]+|[\t ]+$/m', '', $html);
    foreach ($anchors as $href => $index) {
        $html .= "\n[$index] $href";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following HTML fragment: <span name=foo class=foo-class> <input name=foo value=0 id=foo2_0 type=radio> <label
I need to split long string into a array with following constrains: The input
In this fiddle , with the following HTML and CSS: Html <input type=text></input> CSS
I'm currently using the following regex to validate currency in my html input form
I have the following HTML w/Javascript code (note: this only works in Internet Explorer):
I am trying to achieve html similar to following with MVC3 + Razor: <input
HTML: <div id=fields> <input type=text name=someField /> <!-- NOTE: There are some more fields
Note: Boost's archive scheme is based on symmetrical input and output archive classes. It's
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code

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.