Mobilpadde
Mobilpadde

Reputation: 1881

Get a part of content from a url

I don't know if you are able to do this, but...

Is there anyway to get a part of the content from a URL ?

Like if I have this:

<protected>false</protected>
<followers_count>6</followers_count>
<profile_background_color>00afe0</profile_background_color>

(The whole content is from Twitter)

But only want the <followers_count>6</followers_count> part?

Somebody who know how to do this?
(I hope you guys know what I mean by this)


PS. Anything above is JUST an example
PSS. Not just gona use this for xml, etc this format too (Which I don't know the name of)

Yes, I know that I could just use the file_get_contents-offset and maxlen thing, but the problem is, that I don't always know, where the part I'm looking for is.

Upvotes: 0

Views: 165

Answers (3)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

$data = file_get_contents('http://twitter.com/users/show.xml?screen_name=SlogaNator');

$xml = simplexml_load_string($data);

echo $xml->followers_count; # 6

"Let's give this guy minuses because he doesn't know you shouldn't parse XML with RegExp" version...

preg_match('#<followers_count>([0-9]+)</followers_count>#is', $xml, $matches);
echo $matches[1]; # 6

Upvotes: 0

user142162
user142162

Reputation:

What you're looking at is called XML. PHP has a built-in XML parser which will allow you to extract the data you need.

$xml = simplexml_load_file(
          'http://twitter.com/users/show.xml?screen_name=SlogaNator'
       );

$follower_count = (int) $xml->followers_count; // int(6)

Upvotes: 1

Justin Lucas
Justin Lucas

Reputation: 2321

You could use a native XML parser such as SimpleXML to find the value of a specific piece of content.

Upvotes: 4

Related Questions