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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:11:21+00:00 2026-05-29T17:11:21+00:00

I receive a text formatted as html. I want to restrict anchor tag’s urls

  • 0

I receive a text formatted as html. I want to restrict anchor tag’s urls to be only from my domain replacing the old links with “xxx” (or smth’ else).
Input: “<a href='otherdomain'>text</a>“
Output: “xxx”
I am using regexp to achieve this though I’m kind of stuck here:

$pattern ='/<a.*href=[\'|\"]http.?:\/\/[^mydomain.*\"\']*[\'|\"].*<\/a>/i';
$replace ='xxx';
echo preg_replace($pattern, $replace, $string); 

What is wrong here?

  • 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-29T17:11:22+00:00Added an answer on May 29, 2026 at 5:11 pm

    When you do [^mydomain.*\"\'] you are saying “match any character except a literal ‘m’, ‘y’, ‘d’, ‘o’, …, ‘.’, ‘*’, etc.

    Try something like:

    #<a [^>]*\bhref=(['"])http.?://((?!mydomain)[^'"])+\1 *>.*?</a>#i
    

    Notes:

    • I turned your a.*href to a [^>]*\bhref to make sure that the ‘a’ and ‘href’ are whole words and that the regex doesn’t match over multiple tags.
    • I changed the regex delimiter character to ‘#’ instead of ‘/’ so you don’t have to escape the / any more
    • Note the ((?!mydomain)[^'"])+. This means “match [^'”]+ that isn’t mydomain”. The (?! is called a negative look-ahead.
    • Note the \1. This makes sure that the closing quote mark for the URL is the same as the opening quote mark (see hwo the first set of brackets captures the ['"]?). You’d probably be fine without it if you prefered.

    For PHP (updated because I always mix up when backslashes need to be escaped in PHP — see @GlitchMr’s comment below):

    $pattern = '#<a [^>]*\bhref=([\'"])http.?://((?!mydomain)[^\'"])+\1 *>.*?</a>#i';
    

    See it in action here, where you can tweak it to your purposes.

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

Sidebar

Related Questions

There is a web site http://www.pringit.com i want to receive text messages from mobile
I have an text reader app that is designed to receive intent from Android
I'm setting up my site to receive info from people via text message. The
I receive text from a *.csv file in any date format For example: dd/mm/yy
When I receive html text by ajax in asp.net application it looks like: &lt;span%20style='color:green;font-weight:bold'&gt;%20Text%20Msg&lt;/span&gt;
I would like to create a web application which receive status(text) from android. The
In our application, we receive text files ( .txt , .csv , etc.) from
I'm looking for a mobile software that will send/receive text messages from mobile phone
I have created JAVA GUI that will receive text file(.txt) from other client and
In my application I receive a text from a database file using a cursor.

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.