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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:53:00+00:00 2026-05-15T18:53:00+00:00

I am trying to pull the anchor text from a link that is formatted

  • 0

I am trying to pull the anchor text from a link that is formatted this way:

<h3><b>File</b> : <a href="/en/browse/file/variable_text">i_want_this</a></h3>

I want only the anchor text for the link : “i_want_this”

“variable_text” varies according to the filename so I need to ignore that.

I am using this regex:

<a href=\"\/en\/browse\/file\/variable_text\">(.*?)<\/a>

This is matching of course the complete link.

  • 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-15T18:53:01+00:00Added an answer on May 15, 2026 at 6:53 pm

    PHP uses a pretty close version to PCRE (PERL Regex). If you want to know a lot about regex, visit perlretut.org. Also, look into Regex generators like exspresso.

    For your use, know that regex is greedy. That means that when you specify that you want something, follwed by anything (any repetitions) followed by something, it will keep on going until that second something is reached.

    to be more clear, what you want is this:

    1. <a href="
    2. any character, any number of times (regex = .* )
    3. ">
    4. any character, any number of times (regex = .* )
    5. </a>

    beyond that, you want to capture the second group of “any character, any number of times”. You can do that using what are called capture groups (capture anything inside of parenthesis as a group for reference later, also called back references).

    I would also look into named subpatterns, too – with those, you can reference your choice with a human readable string rather than an array index. Syntax for those in PHP are (?P<name>pattern) where name is the name you want and pattern is the actual regex. I’ll use that below.

    So all that being said, here’s the “lazy web” for your regex:

    <?php
    $str = '<h3><b>File</b> : <a href="/en/browse/file/variable_text">i_want_this</a></h3>';
    $regex = '/(<a href\=".*">)(?P<target>.*)(<\/a>)/';
    preg_match($regex, $str, $matches);
    
    print $matches['target'];
    ?>
    
    //This should output "i_want_this"
    

    Oh, and one final thought. Depending on what you are doing exactly, you may want to look into SimpleXML instead of using regex for this. This would probably require that the tags that we see are just snippits of a larger whole as SimpleXML requires well-formed XML (or XHTML).

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

Sidebar

Related Questions

No related questions found

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.