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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:14:50+00:00 2026-06-12T20:14:50+00:00

<a href=http://www.example.com/foo/bar/ title=bar>Example</a> How can I replace bar only in the href attribute? Thank

  • 0
<a href="http://www.example.com/foo/bar/" title="bar">Example</a>

How can I replace “bar” only in the href attribute?

Thank you!

  • 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-12T20:14:51+00:00Added an answer on June 12, 2026 at 8:14 pm

    Let me start off by saying, regular expressions are not the right tool. (At least not to find the attributes; for the replacement, probably…)

    However, here is a regular expression solution anyway, but I cannot guarantee that it will work on all valid DOMs.

    $newStr = preg_replace(
        '/(<a\s+[^>]*href="[^"]*)bar([^"]*"[^>]*>)/',
        '$1newString$2',
        $str);
    

    Some explanation for the regex:

    I start with a capturing group that ensures we are inside the href attribute of an a-Tag. The reason we capture this is, that it makes the replacement a bit cleaner. The \s+[^>]* allows for other attributes to come first, but not for the tag to close. The [^"] allows for more content in the href attribute to come first, but not for the attribute to close. Then we have bar. And then we add some more stuff before closing the attribute and then the tag.

    The replacement then simply uses captured groups $1 and $2 (which contain everything around bar, which we had to use to make sure it’s in the right place) but inserts the new string in between (where bar used to be).

    Note, this will especially break if you have attributes containing > before the href-attribute!

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

Sidebar

Related Questions

<a href=http://www.example.com/foo/bar/ title=foo> <img src=http://www.example.com/foo/bar/ alt=foo /> </a> How can I preg_replace the word
How to replace HREF value with dynamic value in java <a href=\http://www.example.com\> with <a
i am using the Ajaxy jQuery plugin: http://balupton.com/sandbox/jquery-ajaxy/ when i visit http://www.example.com/#/foo/ the content
This is a nasty one. I have an external link. <a href=http://www.example.com target=_blank> now
Observe: <span class=description> Not This person? <a href=http://www.example.com target=_self>Click Here</ > </span> I want
Our site contains href links to various subdomains: foo.mysite.com, bar.mysite.com. For testing purposes, I'd
I'm using this example to fetch links from a website : http://www.merchantos.com/makebeta/php/scraping-links-with-php/ $xpath =
I have a webpage - http://www.example.com/ - where I have a button. On click
I want to get the location and the first folder, like: http://www.example.com/test/ var $location
THE EXAMPLE First and foremost, here's my code and problem: http://www.nathanstpierre.com/MBX/showoff.html THE ISSUE So

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.