There are links

<a href="link">название ссылки</a> <a href="link" title="подпись">название ссылки</a> 

And similar, the attributes inside can be different.

I need to find and replace only the link name of all links that are in the line.

It is desirable regular. I would be grateful for the help.

1 answer 1

you need to find and replace only the link name of all links that are in the line

If you only need a search, then you can:

 $str = '<a href="link">название ссылки 1</a> <a href="link" title="подпись">название ссылки 2</a>'; preg_match_all('~<a[^>]*>(.+?)</a>~', $str, $arr); var_dump( $arr[1] ); 

Result:

 array (size=2) 0 => string 'название ссылки 1' 1 => string 'название ссылки 2' 

If search with replacement, then:

 $repl = 'Новое название ссылки'; $str = preg_replace('~(<a[^>]*>).+?(</a>)~', "$1{$repl}$2", $str); var_dump( $str ); 

Result:

 <a href="link">Новое название ссылки</a> <a href="link" title="подпись">Новое название ссылки</a>