Modification or improvement of this code?

This: select all links from the string, the link text is replaced by the path before the link add text links. Links can be any number.

Solution:
// search string
$str = "a Long established fact that in assessing the design and composition of readable text interferes with concentration.
<a href="\"http://example1.com/\"">Some text ссылки1</a>Lorem Ipsum used because 
that provides a more or less standard filling pattern, as well as real
 the distribution of letters and spaces in the paragraphs, <a href="\"http://example2.com/\"">Some text ссылки2</a> 
which is not obtained by simple duplication.
<a href="\"http://example3.com/\"">Some text ссылки3</a> 
It has long been established that in assessing the design and composition of readable text interferes with concentration. 
Lorem Ipsum is used because he provides a more or less standard filling pattern, 
as well as the actual distribution of letters and spaces in a paragraph that is not obtained by simple duplication.";

// get all links
$reg = '/<a[^>]+href="([^ >]+)">(.*?)<\/a>/';
preg_match_all($reg, $str, $arr);

// replace text on a path
$text_way = str_replace($arr['2'], $arr['1'], $arr['0']);

// add the text before the link
$text_a = array_map(function($f, $s){return "$f $s";}, $arr['2'], $text_way);

// replace links in a string
$str = str_replace($arr['0'], $text_a, $str);

echo $str;</a[^>

Maybe there is a more simple solution to this problem?
July 12th 19 at 17:26
2 answers
July 12th 19 at 17:28
https://github.com/Imangazaliev/DiDOM :

$document = new Document($str);

$elements = $document->find('a');

foreach($elements as $element) {
$element->setValue($element->href);
}
Broken link - gianni commented on July 12th 19 at 17:31
: had colon removed, fixed - jason_Hayes commented on July 12th 19 at 17:34
July 12th 19 at 17:30
On this subject there is a famous comment on stackoverflow:
stackoverflow.com/questions/1732348/regex-match-op...

For parsing html, use intended for this library:
https://github.com/symfony/dom-crawler
https://github.com/sunra/php-simple-html-dom-parser
etc.
Alas, it is not parsing html, $str taken from the base. - gianni commented on July 12th 19 at 17:33
so the source is not important, links the html. - jason_Hayes commented on July 12th 19 at 17:36

Find more questions by tags PHP