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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:05:46+00:00 2026-06-17T07:05:46+00:00

How could I use RegEx to find <a class=modal> and replace it with <a

  • 0

How could I use RegEx to find <a class="modal> and replace it with <a class="modal-link" ?

I tried:

preg_replace('/<a class="(.*?)">(.*?)<\/a>/i', '<a class="$1-link">$2</a>', $input);

but it’s not working. Please can someone point me out to the right solution ?

By the way: I’am styling Joomla 2.5 frontpage edit content page, where the approved user can post an article. That string where i want to apply on my regex comes from:

<?php echo $this->form->getInput('image_intro', 'images'); ?>

it generates this HTML:

<a class="modal" ...>Choose</a>
  • 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-17T07:05:48+00:00Added an answer on June 17, 2026 at 7:05 am

    A non-regex based solution using PHP’s DOMDocument class would be:

    // Load up the HTML in DOMDocument
    $doc = new DOMDocument;
    $doc->loadHTML( '<a class="modal">Stuff</a>'); // Or: $doc->loadHTML( $this->form->getInput('image_intro', 'images'));
    
    // Create a new XPath object to find the correct <a> tags
    $xpath = new DOMXPath( $doc);
    
    // Iterate over all <a class="modal"> tags
    foreach( $xpath->query( "//a[@class='modal']") as $a) {
        // Set the class attribute correctly
        $a->setAttribute( 'class', $a->getAttribute( 'class') . '-link');
        echo $doc->saveHTML( $a); // Print the fixed <a> tag
    }
    

    This will output something similar to:

    <a class="modal-link">Stuff</a>
    

    This method is more robust and stable than a regular expression based approach, especially if you’re getting HTML from user input.

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

Sidebar

Related Questions

I know I could use PHP to do this, but wanted to find out
How could i use regex to find this table in a page (need to
Could use some help with Regex searching with NetBeans 7.01's find function. I'm trying
could someone help me with the regex pattern that i could use to match
What would be the regex I could use to completely remove a css rule
I'm setting up some goals in Google Analytics and could use a little regex
UPDATE: Thanks for all the great responses! I tried many different regex patterns but
I'm trying to use RegEx to find a pattern within a pattern. Specifically what
I need help with a REGEX that will find a link that comes in
I need help using regular expressions. Ihave read the Java Regex notes, but could

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.