Regex PHP, match all links to specific text

I am looking for a regular expression in PHP that will match an anchor with specific text on it. For example, I would like to get bindings with the text mylink, for example:

<a href="blabla" ... >mylink</a>

Therefore, it must correspond to all anchors, but only if they contain certain text. Therefore, it must match these lines:

<a href="blabla" ... >mylink</a>

<a href="blabla" ... >blabla mylink</a>

<a href="blabla" ... >mylink bla bla</a>

<a href="blabla" ... >bla bla mylink bla bla</a>

but not this one:

<a href="blabla" ... >bla bla bla bla</a>

Because this one does not contain the word mylink.

Also, this should not coincide: "mylink is string"because it is not an anchor.

Any idea?

Thanx granit

+3
source share
4 answers

Try the parser instead:

require_once "simple_html_dom.php";

$data = 'Hi, I am looking for a regular expression in PHP which would match the anchor with a 
specific text on it. E.g I would like to get anchors with text mylink like: 
<a href="blabla" ... >mylink</a>

So it should match all anchors but only if they contain specific text So it should match t
hese string:

<a href="blabla" ... >mylink</a>

<a href="blabla" ... >blabla mylink</a>

<a href="blabla" ... >mylink bla bla</a>

<a href="blabla" ... >bla bla mylink bla bla</a>

but not this one:

<a href="blabla" ... >bla bla bla bla</a> Because this one does not contain word mylink.

Also this one should not match: "mylink is string" because it is not an anchor.

Anybody any Idea? Thanx Granit';

$html = str_get_html($data);

foreach($html->find('a') as $element) {
  if(strpos($element->innertext, 'mylink') === false) {
    echo 'Ignored: ' . $element->innertext . "\n";
  } else {
    echo 'Matched: ' . $element->innertext . "\n";
  }
}

which produces the conclusion:

Matched: mylink
Matched: mylink
Matched: blabla mylink
Matched: mylink bla bla
Matched: bla bla mylink bla bla
Ignored: bla bla bla bla

simple_html_dom.php : http://simplehtmldom.sourceforge.net/

+9

( , , "mylink" )

<\s*a\s+[^>]*>[^<>]*mylink[^<>]*<\s*\/a\s*>

. HTML . Regex . ( , , " > ", )

, php - escape-, .

regexpal.com

::
\ s * -
\ s + - /
[^ > ] - , ' > '
[^ < > ] - , '<' ' > '

UPDATE: "/" php m/regex/

+1
if (preg_match('%<\s*a\s+href="blabla"[^>]*>(.*mylink.*)<\s*/a>%', $text, $regs)) {
    $result = $regs[1];
} else {
    $result = "";
}

$regs[0] $regs[1]

0
/<a[^>]*>([^<]*mylink[^<]*)<\/a>/

, , (<a href="/xyz">xyz <i>mylink</i> aaa</a>), .

0

Source: https://habr.com/ru/post/1721696/


All Articles