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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:25:46+00:00 2026-06-12T05:25:46+00:00

I use preg_replace function that has to clear all the rel and target attributes

  • 0

I use preg_replace function that has to clear all the rel and target attributes from the $body and to replace them with other values. Here it is:

    $patterns = array();
    $patterns[] = '/<a(.*) rel="([^"]*)"(.*)>/';
    $patterns[] = '/<a(.*) target="([^"]*)"(.*)>/';
    $patterns[] = '/<a(.*)>/';

    $replacements = array();
    $replacements[] = '<a$1$3>';
    $replacements[] = '<a$1$3>';
    $replacements[] = '<a rel="nofollow" target="_blank"$1>';

    $body = preg_replace($patterns,$replacements,$body);

The problem is that it does not match single and no-quotes.
Also if there is any better approach for clearing the rel and target attributes from the links and setting them with others, please advise.

Thanks

EDIT: $body:

    $body = '<a href="TEST">Link1</a>
      <a href="TEST" rel=\'lqlqlq\'>Link2</a>
      <a href="TEST" target="_blank" rel="lqlqlq">Link3</a>
              <a href="TEST" target=_blank rel=lqlqlq>Link4</a>';

And also every other option for working links due to the text is user defined and probably some users are going to cheat. My goal – all of the links in $body to be with defined rel and target attributes no matter what the user has entered.

  • 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-12T05:25:47+00:00Added an answer on June 12, 2026 at 5:25 am

    If you don’t want to match anything else at the same time I suggest you don’t and preserve system resources.

    $patterns[] = "/rel=[\"\']?([\w]+)[\"\']?/";
    $patterns[] = "/target=[\"\']?([_a-zA-Z]+)[\"\']?/"
    

    I’m not the best at REGEX but as far as I know this will save you some time.

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

Sidebar

Related Questions

I'm having trouble with preg_replace and I'm not sure that I use correct function.
I'm trying to figure out an preg_replace() (php) style function that I can use
I am trying to use preg_replace function to parse this table in a Html
I'm busy with a website and for an element I use preg_replace to replace
With PHP, I'd like to use a preg_replace() filter for passwords such that the
Can you use two regex in preg_replace to match and replace items in an
How can I make a regex pattern for use with the PHP preg_replace function
I use the following function to filter numbers from a string (including some special
I have a problem with the preg_replace function. In my code i will use
I can't quite figure out the right pattern to use with PHP's preg_replace function.

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.