php - Get an url with preg_match_all (simple) -


I do not know why I can not get some URL from some source code from a website with preg_match, maybe it I am doing it wrong, I tried to do it in many ways but I can not find it ...

The problem is that I am just trying to get the URL from a source code That looks like this:

  & lt; H2 & gt; & Lt; A href = "http://www.website.com/index.php" h = "id = sERP, 5085.1" & gt; Website Name & lt; / A & gt; & Lt; / H2 & gt;  

So how do I want to go into a variable

I was doing something like this:

  preg_match_all ('/ / Lt ; H2 & gt; & lt; a href = ". *" & Gt; / ', $ text, $ m); $ / Text> $ text source code, its actually a long website source code, so I just want to get href from the tag & lt; A> Those that are in the tag & lt; H2>. . I hope you guys can help me  

You have asked for a regular expression here, But this is not the right tool to parse HTML, use it for:

  $ html = & lt; & Lt; & Lt; Data & lt; H2 & gt; & Lt; A href = "http://www.website.com/index.php" h = "id = sERP, 5085.1" & gt; Website Name & lt; / A & gt; & Lt; / H2 & gt; & Lt; H2 & gt; & Lt; A href = "http://www.example.com" & gt; Example site & lt; / A & gt; & Lt; / H2 & gt; & Lt; H1 & gt; & Lt; A href = "http://www.bar.com" & gt; Bar & lt; / A & gt; & Lt; / H1> & Lt; A href = "http://www.foo.com" & gt; Foo & lt; / A & gt; Information; $ Dom = new dome document; $ Dom- & gt; LoadHTML ($ HTML); // Load your HTML data. $ Xpath = New DOMXPath ($ dom); Forex Currency ($ xpath- & gt; Query ("// h2 / a") as $ Tag) {$ links [] = $ tag- & gt; GetAttribute ('href'); } Print_r ($ link);  

Output

  array ([0] => http://www.website.com/index.php [1] = & gt; ; Http: //www.example.com)  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -