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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:27:09+00:00 2026-06-10T05:27:09+00:00

I have a text with multiple links in it. One single link looks like

  • 0

I have a text with multiple links in it. One single link looks like

<a href="http://www.mydomain.com/files/test1.pdf" target="_blank">My link</a>

First, I would like to wrap div tag around all href tags but only if href is beggining with http://www.mydomain.com/files/. So the link above (because it starts with that prefix) should become

<div class="myLink">
   <a href="http://www.mydomain.com/files/test1.pdf" target="_blank">My link</a>
</div>

Then depending on file extension (ending characters of the href) add an image before a link so it becomes

<div class="myLink">
   <img src="pdf_file_icon.png" alt="File type icon" />
   <a href="http://www.mydomain.com/files/test1.pdf" target="_blank">My link</a>
</div>

And optionally add some my text after the link so the final version becomes

<div class="myLink">
   <img src="pdf_file_icon.png" alt="File type icon" />
   <a href="http://www.mydomain.com/files/test1.pdf" target="_blank">My link</a>
   <span class="fileSize">1.3 Mb</span>
</div>

I need solution for both PHP and JavaScript/jQuery (I think it’s easier for jQuery because of it’s selector capabilites?).


What I want to get is to move from regular text link to something like this (don’t bother with file size, it’s pre-calculated so it’s just like any other regular/dummy text)

enter image description here

Edit #1

Here is my try in PHP

$regexp = "<a\s[^>]*href=(\"??)(http:\/\/www\.mydomain\.com\/files\/[^\" >]*?)\\1[^>]*>(.*)<\/a>";
$text = preg_replace("/$regexp/siU", "<div class=\"fileAttachment\"><img src=\"pdf_file_icon.png\" alt=\"File type icon\" /><a href=\"$2\" target=\"_blank\">$3</a></div>", $text);

But I’m not sure how immediately to check for file extension and depending on it insert appropriate image (pdf_file_icon.png)?

  • 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-10T05:27:10+00:00Added an answer on June 10, 2026 at 5:27 am

    Because I’m a bit bored, here’s a jQuery implementation:

    $('a[href^="http://www.mydomain.com/files"]')
        .each(function() {
            var t = $(this);
            var href = $(this).attr('href');
            var ext = href.substring(href.lastIndexOf('.') + 1);
            t.wrap('<div class="mylink"></div>');
            switch (ext) {
                case 'pdf': 
                   t.before('<img src="pdf_file_icon.png" alt="File type icon" />'); 
                   break;
                // Add more types here
            }
            t.after('<span class="fileSize">1.3 Mb</span>');
        }
    );​
    

    http://jsfiddle.net/AK9nG/

    Surely to do it in PHP you can just edit the HTML directly?

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

Sidebar

Related Questions

I have multiple text files with logged data like this: 6/23/09 17:00 0.443 6/23/09
I have text documents like the following which contain single and multiple variables: title::
I have multiple lines of text entered into a single MySQL longtext cell that
I have a program that creates multiple text files of rdf triples. I need
I have a huge text file that I would like to split into multiple
I have multiple rows in a table, in one of which is a link
I have a single line text file of csv values I would like to
I have a text that has multiple occurrences of variables in this format: Example
I have multiple input text fields grouped in divs: <div class=container0> <input class=containerItem0 name=containerItem[0][0]
I have a simple problem while putting text in multiple table! please have a

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.