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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:21:12+00:00 2026-05-12T07:21:12+00:00

I’m using file_get_contents to get a certain file’s contents — so far that is

  • 0

I’m using file_get_contents to get a certain file’s contents — so far that is working.

Now I want to search the file and replace all <a href=" with <a href="site.php?url= before showing the file.

How can I do this? I know I should use some kind of str_replace or even preg_replace. But I don’t know how to actually search and do it for the file I’m getting with file_get_contents.

  • 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-12T07:21:13+00:00Added an answer on May 12, 2026 at 7:21 am

    file_get_contents returns a string containing the file’s content.

    So, you can work in this string using whichever string manipulation function you’d want, like the ones you talked about.

    Something like this, using str_replace, would probably do :

    $content = file_get_contents('http://www.google.com');
    
    $new_content = str_replace('<a href="', '<a href="site.php?url=', $content);
    
    echo $new_content;
    

    But note it will only replace the URL in the href attribute when that attribute is the first one of the <a tag…

    Using a regex might help you a bit more ; but it probably won’t be perfect either, I’m afraid…

    If you are working with an HTML document and want a “full” solution, using DOMDocument::loadHTML and working with DOM manipulation methods might be another (a bit more complex, but probably more powerful) solution.

    The answers given to those two questions might also be able to help you, depending on what you are willing to do :

    • underlinks also
    • file_get_contents – also get the pictures

    EDIT after seeing the comment :

    If you want to replace two strings, you can pass arrays to the two first parameters of str_replace. For instance :

    $new_content = str_replace(
        array('<a href="', 'Pages'), 
        array('<a href="site.php?url=', 'TEST'), 
        $content);
    

    With that :

    • ‘<a href="‘ will be replaced by ‘<a href="site.php?url=‘
    • and ‘Pages‘ will get replaced by ‘TEST‘

    And, quoting the manual :

    If search and replace are arrays,
    then str_replace() takes a value from
    each array and uses them to do search
    and replace on subject . If replace
    has fewer values than search , then an
    empty string is used for the rest of
    replacement values. If search is an
    array and replace is a string, then
    this replacement string is used for
    every value of search .

    If you want to replace all instances of ‘<a href="‘, well, it’s what str_replace does by default 🙂

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

Sidebar

Related Questions

No related questions found

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.