In general, the parsing of the link looks something like this (on the assurance of the Internet):

/<a [^<>]*href=[\'"]([^\'"]+)[\'"][^<>]*>(((?!/si 

But in my case it gives an error: Warning: preg_match () [function.preg-match]: Compilation failed: missing) I was looking for other ways, but everyone comes with errors or does not look for a link. In my case, the link is in the form (quotes may be different or not at all, page, id - abstraction):

 <a href="/page?id=123">подробнее</a> 

So how to parse?

  • @sinneren, did you try to make out the regulars? It is cropped in the middle, the closing tag does not understand, there is no content. Parsing html is better without regulars, since regulars suggest a one-level analysis, and this always leads to frustration. Without frustration, the document must first be divided into tags, then deal with each. - etki
  • html I parse through Simple HTML Dom parser. But he, in turn, also gives html, or I didn’t figure out how to parse the resulting objects further, but it turns out that only through regulars. Through what else can not even imagine. - sinneren
  • Ok, decided through stripos and substr. - sinneren
  • in the article xdan.ru/… write that everything is simple to parse and regulars are not needed. - KoVadim

2 answers 2

Use DOM correctly.

Here is an example :

 $DOM = new DOMDocument(); $DOM->loadXML('<html><body><a href="/page?id=123">подробнее</a><a href="mailto:name@domain.com"><span><b>somename@domain.com</b></span></a><a href="http://domain.com">domain.com</a></body></html>'); $list = $DOM->getElementsByTagName('a'); $link_array = array(); foreach($list as $link){ $href = $link->getAttribute('href'); array_push($link_array, $href); } print_r($link_array); 

And here is the result:

 Array ( [0] => /page?id=123 [1] => mailto:name@domain.com [2] => http://domain.com ) 

I do not like unanswered questions :).

    Option with regexp (JS)

     var a = '<a href="/page?id=123">подробнее</a>'; alert(a.split(/(href=\")*"/g)[1]); 

    only this for a particular case. Odd elements will show the href values. Rewrite to php, I think, is not difficult. PS href can be not only in A tag (often meta).