0

How can I retrieve the result from the $avatar array?
This is what I tried:
I want to get the following(url):
code:

<div id="fotoprofilo"><img src="http://images.instagram.com/profiles/profile_19347213_75sq_0000.jpg"></div>

And php:

$s2 = file_get_contents("http://followgram.me/user");
    $avatar = "";
     preg_match_all('/fotoprofilo\s*"><img src=".*\b">/', $s2, $avatar);
    print_r($avatar[0]);

1) How is the result working?
2) Will it find it? (Because I can't seem to see the value)

Thanks in advance.

knittl
  • 246,190
  • 53
  • 318
  • 364
funerr
  • 7,212
  • 14
  • 81
  • 129
  • 5
    [Don't use regular expressions to parse HTML](http://stackoverflow.com/q/1732348/112968) – knittl Mar 04 '12 at 15:19
  • Also, your server must be configured to allow file_get_contents to retrieve files over http://, per default it only works with local files – knittl Mar 04 '12 at 15:21
  • #knittl It actually works for me :) (the http), I am checking the don't use... Plus, how does the result work?! – funerr Mar 04 '12 at 15:24
  • I agree with knittl - Use one of the **perfectly good XML/HTML parsers** out there – Kian Mar 04 '12 at 15:29

1 Answers1

1

Use an array and brackets for subexpressions:

$s2 = file_get_contents("http://followgram.me/user");
$avatar = array();
preg_match_all('/fotoprofilo\s*"><img src="(.*)">/', $s2, $avatar);
print_r($avatar);

$avatar[1] should contain your URL.

To test regex and preg_match_all you can use http://reghex.mgvmedia.com/. Choose "PHP" as language and check option "global search". Works fine :)

Sebastian vom Meer
  • 5,005
  • 2
  • 28
  • 36