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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:16:07+00:00 2026-06-08T21:16:07+00:00

Here is my string: $str=<p>Some <a href=#>link</a> with <a href=http://whatever.html?bla>LINK2</a> and <a href=http://whatever.html?bla target=_blank>LINK3</a>

  • 0

Here is my string:

$str="<p>Some <a href="#">link</a> with <a href="http://whatever.html?bla">LINK2</a> and <a href="http://whatever.html?bla" target="_blank">LINK3</a> and</p> more html"

I would like to remove the links LINK1 and LINK2 using php to get:

"<p>Some <a href="#">link</a> with and and</p> more html"

Here is what I think is close to what I need:

$find = array("<a(.*)LINK1(.*)</a>", "<a(.*)LINK2(.*)</a>");
$replace = array("", "");
$result=preg_replace("$find","$replace",$str);

This isn’t working. I have searched for days and tried many other options but never managed to get this to work as expected. Also, I don’t really mind if LINK1 and 2 appear as soon as the a tags are removed.

  • 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-08T21:16:08+00:00Added an answer on June 8, 2026 at 9:16 pm

    You are very close to a working solution. The problem you are facing is that regular expressions per default try to match as much as possible. The pattern <a(.*)LINK1(.*)</a> will in fact match the first <a to the last </a> if they have LINK1 inbetween. What you want is just to just get the nearest <a> tag.

    There are a few ways to do this, but I usually go for making the matching ungreedy. Then it will instead try to find the smallest possible matches. Two ways of doing this is to append a ? after the quantifier or using the ungreedy modifier U. I prefer the first one.

    Using ?:

    /<a(.*?)LINK1(.*?)<\/a>/
    

    Using modifier:

    /<a(.*)LINK1(.*)<\/a>/U
    

    Both should work equally well here. The entire source code will thus be as follows (using ?):

    $find = array("/<a(.*?)LINK1(.*?)<\/a>/", "/<a(.*?)LINK2(.*?)<\/a>/");
    $replace = array("", "");
    $result = preg_replace($find, $replace, $str);
    

    And yeah, as noted in other comments you shouldn’t rely on regular expressions for manipulating HTML code (because it is really easy to construct valid HTML code that will go through the expression unnoticed). However, I believe it is perfectly ok if you trust the HTML code that you parse or that the result of this matching is not crucial for other important functions.

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

Sidebar

Related Questions

Here is a simple example; template <typename T> void foo(T t) {} std::string str(some
Here is the code: string str; cin>>str; cout<<first input:<<str<<endl; getline(cin, str); cout<<line input:<<str<<endl; The
static void Main() { string str; str = Console.ReadLine(); while (str != null)//HERE! {
Here is the current code in my application: String[] ids = str.split("/"); When profiling
For example, here is a string representing an expression: var str = 'total =
What's the simplest way, given a string: NSString *str = @Some really really long
While developing my application i came across some comparison stuff here was it: string
Here is my string: String str = <pre><font size=5><strong><u>LVI . The Day of Battle</u></strong></font>
Here is some Java code to reverse a string recursively. Could someone provide an
JSoup seems to be skipping some elements in my HTML string. I am 100%

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.