SaidbakR
SaidbakR

Reputation: 13534

Regular expression match a hyperlink of defined hypertext

I'm trying to find a regex that matches an HTML hyperlink tag that has defined hypertext. For example:

<a href="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a>

I need to match any hyperlink that its hypertext is "The defined text".

I just found the following expression on Regular Expression Library but it does not solved the issue.

<a.+?href\=(?<link>.+?)(?=[>\s]).*?>(?<lnkText>.+?)</a>

I hope a solution that works with PHP.

Upvotes: 3

Views: 1044

Answers (4)

GrahamTheDev
GrahamTheDev

Reputation: 24825

$regex = "/<a\s.*href.*>The defined text<\/a>/"; //corrected as per the comments
$str = '<a href="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a>
    <a href="blah blah" anyAttribute="" anyThingElse="anyValue">The Not-defined text</a>
';

preg_match($regex, $str, $matches);

foreach($matches as $match) echo $match."<br/>";

It will match the whole string from

<a href

to the closing tag.

</a> 

EDIT

TECHNICALLY the regex could just be:

$regex = "/<a\s.*>The defined text<\/a>/" 

in fact this is probably better!

Thanks for the catch in the comments!

Upvotes: 2

Dmitriy.Net
Dmitriy.Net

Reputation: 1500

You can to use this regex:

((?=<a.+href).*(?<=</a>))[^</a]

For global search you must add flag "g" and for disabling case sensitive flag "i"

As for php:

$html = '<a href="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a>
         <a href="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a>
         <a href="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a>
         <a href="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a>
         <a HREF="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a>
         <a anyAttribute="" anyThingElse="anyValue" href="blah blah">The defined text</a>';

preg_match('/((?=<a.+href).*(?<=</a>))[^</a]/gi', $html, $matches);

var_dump($matches);

This solution for getting all links with href attribute, even if several "a" is placed at one line. Example:

<a HREF="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a><a>asdasd</a>

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

TRY this:

DEMO LIVE: https://eval.in/85894

 $p = '/<a.*? (href=".*?").*?>The defined text<\/a>/';
    $str = '
       <a href="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text2</a>
    <a href="blah blah" anyAttribute="" anyThingElse="anyValue">The defined text</a>

    ';
    preg_match_all($p,$str,$m);
    print_r($m[1]);

OUTPUT :

Array
(
    [0] => href="blah blah"
)

Upvotes: 1

Lorenz
Lorenz

Reputation: 2259

$html='<a href="blah blah" anyAttribute="" anyThingElse="anyValue">The
defined text</a>';
preg_match_all("/<a.+?href\=\"([^\"]*)\"[^>]*>The defined Text<\\/a>/",$html,$matches);
foreach($matches[1] as $match) echo "Link found: " . $match;

It returns

Link found: blah blah

If you want the array with all the matches:

$matches[1]

Upvotes: 1

Related Questions