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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:34:23+00:00 2026-05-24T12:34:23+00:00

I need get bbish name3 more bbish name4 more $p = ‘%<a\s+href=my-anchor-name3\s*>(?:.*)</a>%im’; $s =

  • 0

I need get

bbish name3 more

bbish name4 more

$p = '%<a\s+href="my-anchor-name3"\s*>(?:.*)</a>%im';
$s = 'some rubbish
<a href="my-anchor-name1">name</a>more rubbish
more rubbish<a href="my-anchor-name2">name2</a>more rubbish
more rubbish<a href="my-anchor-name3">name3</a>more rubbish
more rubbish<a href="my-anchor-name3">name4</a>more rubbish
more rubbish<a href="my-anchor-name5">name5</a>more rubbish';
$out = preg_match_all($p, $s, $matches, PREG_SET_ORDER);

what am I doing wrong?

  • 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-24T12:34:23+00:00Added an answer on May 24, 2026 at 12:34 pm

    what am I doing wrong?

    You’re not instructing PHP to do what you have indicated that you want to do, is the main flaw.


    Problems

    • You did not create an array into which to deposit the matches;
    • You’re not capturing any backreferences;
    • Your capture inside the a tag is greedy;
    • I suspect that you don’t really want to restrict your href value like that;
    • Your HTML input is very restricted, because you’re using regular expressions to parse HTML…. grrrrr!! *

    Fix

    Try this:

    <?php
    $matches = Array();
    $p = '%(.{0,5})<a\s+href="my-anchor-name3"\s*>(.*?)</a>(.{0,5})%imm';
    $s = 'some rubbish
    <a href="my-anchor-name1">name</a>more rubbish
    more rubbish<a href="my-anchor-name2">name2</a>more rubbish
    more rubbish<a href="my-anchor-name3">name3</a>more rubbish
    more rubbish<a href="my-anchor-name3">name4</a>more rubbish
    more rubbish<a href="my-anchor-name5">name5</a>more rubbish';
    $out = preg_match_all($p, $s, $matches, PREG_SET_ORDER);
    print_r($matches);
    ?>
    

    Output:

    Array
    (
        [0] => Array
            (
                [0] => bbish<a href="my-anchor-name3">name3</a>more 
                [1] => bbish
                [2] => name3
                [3] => more 
            )
    
        [1] => Array
            (
                [0] => bbish<a href="my-anchor-name3">name4</a>more 
                [1] => bbish
                [2] => name4
                [3] => more 
            )
    
    )
    

    Live demo.


    Further work

    You may wish to further restrict what characters may be eaten up in those backreferences.

    And if you don’t want to limit your href values the way you are (and you’re doing it in quite a confusing way at present):

    $p = '%(.{0,5})<a\s+href="my-anchor-name\d+"\s*>(.*?)</a>(.{0,5})%imm';
    

    Like this.


    * The real answer here is that you should not be using regular expressions to parse HTML, which is a well-known fact. Marc has the solution that you should be using.

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

Sidebar

Related Questions

Just need get some vals located in application.ini(main ini) in the Controller plugin I
i have some scripts that need GET and POST values to start, and i
I have two combobox. I need get some value from first combobox1 after combobox1
<div class=myDiv> <p>I need get<strong>this</strong> <a title=And this href=#>but not this</a> </p> <p>And also<strong>This</strong>
I need get H264 stream from x264 encoder and make some kind of live
I'm developing an application for Galaxy Tab, and now I need get some files
I need get the id of an images which is place inside a datatemplate..
I need get all items these have no categories int? categoryId = null; var
I have linq request. I need get item.Title in select. how do this? var
I have two tables with a weak relation. I need get a text value

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.