Hello everyone, help write a regular expression to get the code from the href-attribute

href="watch?v= тут код" 

from a huge string of html content.

  • html is better to disassemble the functions of php.net/manual/en/class.domdocument.php And in general, that /от(.*?)до/ ( /от(.*?)до/ do /от(.*?)до/ - Mike
  • There is a program that extracts the values ​​of the href and / or src attributes. - Sasha Chernykh
  • I tested the program, it extracts anchors and the contents of relative paths. Besides paid. If you need to extract only the links that make up the values ​​of the href or src attributes, the template is: (?<=[href|src]=")https?:\/\/.*?(?=") . Demonstration - Sasha Chernykh
  • HTML parsing is not allowed. Your KO - VladD

2 answers 2

You can do the following:

 <?php $html = 'href="watch?v= тут код"'; preg_match('/href="watch\?v=([^"]+)"/', $html, $out); echo $out[1]; 
  • cheops, why in your example does not necessarily protect the + sign ? ? Thank. - Sasha Chernykh
  • @ SashaBlack, the fact is that we use the symbol [^ "] + - an arbitrary number of characters, with the exception of double quotes, thanks to this we cannot get out for the first one". If we used. +, Then yes it was necessary to change greed with. +?. Sometimes it happens that a single or double quote is used in advance, it is possible to use an expression of the form / href = ["'] watch \? V = ([^"'] +) ["'] / - cheops
  • Accustomed to always protect greedy quantifiers, did not immediately understand this feature. Thank you. - Sasha Chernykh
  • one
    @Artur, isn't it? You do not like so ? - Sasha Chernykh
  • one
    @Artur, not quite understand you. > Is it still possible to search this expression in the id = "result" block? - Please specify what you want to find. - Sasha Chernykh

that's what I'm doing:

  $youtubeUri = "http://www.youtube.com/results?search_type=videos&search_query=".$filmsInfo[$key]['title']; $request = $this->client->request('GET', $youtubeUri); $content = $request->getBody()->getContents(); // Тут весь контент есть с ним всё хорошо. 

Usually I use symfony crawler

 $crawler = new Crawler($content)->filter('#results')->html(); 

But in my cases with youtube, I don’t know why there is a block early-body in which iframe with some Doctype and crawler we only see its content, the other one doesn’t see

  • @ Sasha Chernykh, I have a task to get the code for the movie trailer that will be released. I have a name, the title of the trailer is often the first in the list of videos. ) - Arturas