Like parsu, but only headers are needed:

preg_match( '/<ul class="class1 (statichnii)"><li class="class2 (statichnii)">(.*?)<\/ul><\/li>/is' , $text , $links ); echo $links[0]; 

Initial data:

 <ul class="class1 (statichnii)"> <li class="class2 (statichnii)"><a href="http://random" class="random">Darth Vader</a></li> <li class="class2 (statichnii)"><a href="http://random" class="random">Anakin Skywalker</a></li> </ul> 

Pull out: Darth Vader, Anakin Skywalker.

 <ul> <li><a href="https://www.google.ru/?q=Darth+Vader">Darth Vader</a></li> <li><a href="https://www.google.ru/?q=Anakin+Skywalker">Anakin Skywalker</a></li> </ul> 
  • Anything that the original data under your regular season will not coil? - Qwertiy

2 answers 2

 $html = '<ul class="class1 (statichnii)"> <li class="class2 (statichnii)"><a href="http://random" class="random">Darth Vader</a></li> <li class="class2 (statichnii)"><a href="http://random" class="random">Anakin Skywalker</a></li> </ul>'; $document = new \DOMDocument(); $document->loadHTML($html); foreach ($document->getElementsByTagName('a') as $item) { echo $item->nodeValue , "\n"; } 

See the result .

In PHP, there are native tools for working with the DOM.

    Here is a regular routine for getting the necessary data from the links.

     /<a.*?>\s*([^<\s](?:[^<]*[^<\s])*)\s*</i 

    although the answer from @romeo via DOM is more correct.

    PS Example of the regular work https://regex101.com/r/lK3zF6/1

    • Yes, when using UTF-8, the u modifier should be added to the i modifier. - Visman
    • Thank you very much, this option suits me better. I'm going to test now. - user5419467
    • How to take into account that the collection occurred only from <ul class = "class1 (statichnii)">? preg_match( '/<ul class="class1 (statichnii)"><li class="class2 (statichnii)"><a.*?>\s*([^<\s](?:[^<]*[^<\s])*)\s*</i<\/ul><\/li>/is' , $text , $links ); - user5419467
    • @ user5419467, for this you need another regular expression to select the block <ul class="class1 (statichnii)">...</ul> from the main text and apply the regular <ul class="class1 (statichnii)">...</ul> to it from the answer, or use the answer from romeo and get it through DOM the necessary elements. - Visman