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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:12:58+00:00 2026-05-27T08:12:58+00:00

I am looking for a regular expression in PHP that can replace the href

  • 0

I am looking for a regular expression in PHP that can replace the href attribute of anchor tags and src attribute of IMG, style, scripts, etc when they are internal.

an example: if I am looking at the page http://www.mysite.com and on that page there is an image:

<img src="/images/picture.gif /> then I want to be able to change that into:

<img src="http://mysite.com/images/picture.gif />

The same thing for anchor tags: <a href="otherpage.php" >foo</a> should be changed to

<a href="http://mysite.com/otherpage.php" >foo</a>

Also, it should be able to work on other elements that have a src= or a href= attribute, and it should work on elements that have one or more other attributes as well (eg <img class="test" src="/images/picture.gif alt="some picture" />)

I tried something like

preg_replace("/src=[\"']([\/])(.*)?[\"'] /", "src='".$domain."/$2'", $htmldata);

but this did not work well. It took the src attribute, but it captured all the attributes after the src as well. Also, it didn’t capture strings that did not start with a / (e.g. src="image.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-05-27T08:12:59+00:00Added an answer on May 27, 2026 at 8:12 am

    Change greediness with U modifier and allow for zero leading slashes:

     /(?:src|href)\s*=\s*[\"\'](?:\/*)([\w\.]*)[\"\']/U
    

    …and since you don’t need src|href or the leading slash as a backreference, match but omit them with ?:

    Then, the image name becomes $1 instead of $2.

    My use of the U modifier on the whole pattern is because, when ? is otherwise present as in ?: and I don’t need additional granularity, my eyes see it more clearly.

    Although, as others have pointed out, doing this by regex likely isn’t the Best One True Answer… 🙂

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

Sidebar

Related Questions

I am looking for a regular expression using preg_match_all in PHP 5 that would
I'm looking for a regular expression that will replace strings in an input source
I'm looking for a regular expression that will match all strings EXCEPT those that
I am looking for a regular expression that will test for matches against a
I am looking for a regular expression for c# asp.net 3.5 that will fail
I am looking for help in creating a Regular Expression that validates a URL
I'm looking for tags that end in a specific way using regular expressions in
Looking for a regular expression that match the following relative URLs: All urls starts
I'm looking for a regular expression that will match text given the following requirements:
I'm looking specifically for a regular expression that will grab the last term of

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.