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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:18:52+00:00 2026-05-27T05:18:52+00:00

I am still relatively new to Regular Expressions and feel My code is being

  • 0

I am still relatively new to Regular Expressions and feel My code is being too greedy. I am trying to add an id attribute to existing links in a piece of code. My functions is like so:

function addClassHref($str) {
//$str = stripslashes($str);
$preg = "/<[\s]*a[\s]*href=[\s]*[\"\']?([\w.-]*)[\"\']?[^>]*>(.*?)<\/a>/i";
preg_match_all($preg, $str, $match);
foreach ($match[1] as $key => $val) {
    $pattern[] = '/' . preg_quote($match[0][$key], '/') . '/';
    $replace[] = "<a id='buttonRed' href='$val'>{$match[2][$key]}</a>";
}
return preg_replace($pattern, $replace, $str);
}

This adds the id tag like I want but it breaks the hyperlink. For example:

If the original code is : <a href="http://www.google.com">Link</a>

Instead of <a id="class" href="http://www.google.com">Link</a>

It is giving
<a id="class" href="http">Link</a>

Any suggestions or thoughts?

  • 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-27T05:18:52+00:00Added an answer on May 27, 2026 at 5:18 am

    You’ve got some overcomplications in your regex 🙂

    Also, there’s no need for the loop as preg_replace() will hit all the instances of the search pattern in the relevant string. The first regex below will take everything in the a tag and simply add the id attribute on at the end.

    $str = '<a href="http://www.google.com">Link</a>' . "\n" .
    '<a href="http://www.yahoo.com">Link</a>' . "\n" .
    '<a href="http://www.microsoft.com">Link</a>';
    
    $p = "{<\s*a\s*(href=[^>]*)>([^<]*)</a>}i";
    $r = "<a $1 id=\"class\">$2</a>";
    
    echo preg_replace($p, $r, $str);
    

    If you only want to capture the href attribute you could do the following:

    $p = '{<\s*a\s*href=["\']([^"\']*)["\'][^>]*>([^<]*)</a>}i';
    $r = "<a href='$1' id='class'>$2</a>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this is a simple question, but I am still relatively new
I'm relatively new to TDD, and still trying to learn to apply some of
I'm still relatively new to XCode. I'm trying to build an iPhone application and
I'm still relatively new to python, 1-2 years of solo-learning, and am trying to
I'm still relatively new to regex. I'm trying to find the shortest string of
still relatively new to C#, im trying to get my head around the background
I'm still relatively new to C++ and programming, but having a good time learning.
I'm still relatively new to Java and Android development, so I'm still unfamiliar with
I'm still relatively new to hadoop, and I've been learning a bit about it
I am relatively new to WiX. It is a great tool, but I still

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.