There is a line:

<div style="float:left;max-width:280px;"><a href="/url.htm" alt="Операция 

Need to get:

 /url.htm 

Additional question:

There is a regular expression constructor in the zenposter. The expression in it for this situation looks like this:

 (?<=\ \ <div\ style="float:left;max-width:280px;"><a\ href=").*(?="\ alt) 

How to write the same, only for php?

I'm new to PHP, so I just started to figure it out.

  • Use htmldom for this and not a regular season - Naumov

3 answers 3

$ html - the string that we parse, $ m - the results

 preg_match_all('#href="(.*?)"#us', $html, $m); 
     preg_match('#<\s?a\s+href=["']([^"']*)["']#ui', $data, $result); 

    The version is slightly upset, but sometimes there are some upset layouts.

    • Your option is slightly curved. Read character classes for ru.wikipedia.org/wiki/… - Visman
    • Visman, yes, you are right. removed the "sticks" .... hopefully it was meant - smART_dog
     $data = '<div style="float:left;max-width:280px;"><a href="/url.htm" alt="Операция'; $pattern = '~href="(.*)" ~'; preg_match($pattern, $data, $out); print_r($out[1]); 

    instruction here

    http://php.net/manual/ru/reference.pcre.pattern.syntax.php

    or here http://www.rexegg.com/regex-quickstart.html

    • Slightly add the line to the right and get in response /url.htm" alt="Операция sandbox.onlinephpfunctions.com/code/… - Visman
    • $ pattern = '~ href = "(. *?)" ~'; - Leo Tahk
    • And if the result is not one on the page, then how to get several pieces in different variables? - LEX
    • I changed it a bit for myself and the regular works with the whole HTML page. Everything is good. Only now I suffer to get some results. - LEX
    • for all tags of the same name from the page you can use preg_match_all('~<'.$tag.'>(.*)</'.$tag.'>~', $data, $out, PREG_SET_ORDER); - Leo Tahk