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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:36:42+00:00 2026-05-16T14:36:42+00:00

i want to parse all links in html document string in php in such

  • 0

i want to parse all links in html document string in php in such way: replace href=’LINK’ to href=’MY_DOMAIN?URL=LINK’, so because LINK will be url parameter it must be urlencoded. i’m trying to do so:

preg_replace('/href="(.+)"/', 'href="http://'.$host.'/?url='.urlencode('${1}').'"', $html);

but ‘${1}’ is just string literal, not founded in preg url, what need i do, to make this code working?

  • 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-16T14:36:43+00:00Added an answer on May 16, 2026 at 2:36 pm

    Well, to answer your question, you have two choices with Regex.

    You can use the e modifier to the regex, which tells preg_replace that the replacement is php code and should be executed. This is typically seen as not great, since it’s really no better than eval…

    preg_replace($regex, "'href=\"http://{$host}?url='.urlencode('\\1').'\"'", $html);
    

    The other option (which is better IMHO) is to use preg_replace_callback:

    $callback = function ($match) use ($host) {
        return 'href="http://'.$host.'?url='.urlencode($match[1]).'"';
    };
    preg_replace_callback($regex, $callback, $html);
    

    But also never forget, don’t parse HTML with regex…

    So in practice, the better way of doing it (The more robust way), would be:

    $dom = new DomDocument();
    $dom->loadHtml($html);
    $aTags = $dom->getElementsByTagName('a');
    foreach ($aTags as $aElement) {
        $href = $aElement->getAttribute('href');
        $href = 'http://'.$host.'?url='.urlencode($href);
        $aElement->setAttribute('href', $href);
    }
    $html = $dom->saveHtml();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse an html document after it has loaded and discover all
My php script uses php simplehtmldom to parse html and get all the links
I want to parse the following XML document to resolve all entities in it:
I want to parse a text for some links (url and phone numbers) and
Possible Duplicate: How to parse HTML with PHP? Grabbing the href attribute of an
I'm trying to parse this XML I want to get a list of all
I want to parse a X.509 certificate in php. The certificate is in DER-encoded
The following PHP code uses cURL, XPath and displays all the links on a
Possible Duplicate: How to parse and process HTML with PHP? Here is what I'm
I have a script that parses an html page for all the links within

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.