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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:31:56+00:00 2026-06-17T15:31:56+00:00

I have a long string that contains multiple HTML links that looks like this:

  • 0

I have a long string that contains multiple HTML links that looks like this:

<a href="example.com">My Link></a>

or

<a href="http://example2.com">A different Link</a>

etc.

I need to rewrite these links in PHP so that they send the traffic thru my redirector, so I can tell users they are now leaving to an external domain, etc. My redirector is located at mydomain.com/leaving.php. What I want to do is rewrite these links to something like this:

<a href="http://www.mydomain.com/leaving.php/[URL ENCODED LINK FROM ABOVE]">My Link>

Since not all urls have http:// to begin with I think I need to first strip those from all href links.

How can I grep the correct HTML links (ignoring image src) and url_encode them, and place them back in the original string.

EDIT: Just to clearify, I am not looking for help with the redirection part, just how to replace several URLs within a large string when they sometimes have http

  • 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-17T15:31:57+00:00Added an answer on June 17, 2026 at 3:31 pm

    This is just a pseudo that you can modify it as you need.

    You need first a .htaccess file that contents following lines;

    RewriteEngine On
    RewriteRule ^leaving/(.*)$ leaving.php?url=$1 [L]
    

    And in leaving.php;

    $url = trim(urldecode($_GET['url']));
    // check url is exists
    if ($url == '') {
        header('Location: http://www.mydomain.com/');
        exit;
    }
    // add http if needs
    if (substr($url, 0, 7) != 'http://') {
        $url = 'http://'. $url;
    }
    
    // send it to target
    header('Location: '. $url);
    exit;
    

    UPDATE:

    If you trying this on server-side, it’s useless, cos if PHP sent output once, then u cannot use preg's anymore. So, if want to do this on client-side, following code or somthing like will help you.

    var links = document.getElementsByTagName("a"),
        link, href, i = 0;
    while (link = links[i++]) {
        // get real url applying getAttribute with "2" params
        if ((href = link.getAttribute("href", 2)) !== null
                // skip non-href links
                && href.charAt(0) !== "#") {
            // add http if not exists
            if (href.substring(0, 7) !== 'http://') {
                href = "http://"+ href
            }
            link.href = "http://www.mydomain.com/leaving/"+ href;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long string that contains information like this: |infoId-*-info||infoId-*-info||infoId-*-info| ... I use
I have a long string of comments that I'd like to split into multiple
i have a long string of date . looks like that : Mon Jun
I have a long string of HTML that contains <p> <img> <span> and a
I have a string that contains something like this: ##### abc 'foo' /path/to/filename:1 #####
I have a long string that contains multiple css classes. With regex I would
In JavaScript I have a var str = .a long string that contains many
I have a variable that contains a long string that represents an XML document.
I have a variable that contains the string 'long'. How can I create a
I have a long string that contains text. I want to extract every word

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.