Jump to content

having trouble parsing data from page


tobykw13

Recommended Posts

Im trying to parse a short amount of text off of  a web page and Im having some difficulties. I am very new to PHP so this all seems kind of foreign to me. Ive read up on screen scraping and parsing and what not and found some script that seemed to fit what I was wanting to do, and have had some success messing with it, but at the same time havent figured out how to do what I want. Looking at the page source for the page there is a string of text as follows:

 

...= {"UserId":257367900,"DisplayFriendId":13287930,"IsLoggedIn":true};

 

I want to extract the number 13287930. This line is found in the body of the html. I used the following code to try and parse out this number:

 

<?php

 

// the adress of the remote file to read

$url = 'http://www.myspace.com/isabellebanham';

 

// you have to backslash certain characters !

$start = '<html>';

$end = '<\/html>';

 

$fp = fopen($url, 'r') or die('Unable to open file '.$url.' for reading');

$cont = '';

 

while(!feof($fp))

{

$buf = trim(fgets($fp, 4096));

$cont .= $buf;

}

 

preg_match('/'.$start.'(.*)'.$end.'/s', $cont, $match);

 

echo '

<p>

here is the output of '.$url.':<br />

<strong>'.$match[1].'</strong>

</p>';

 

?>

 

The output displays the entire page. As I said I am very new to PHP so this looks almost foreign to me so bare with me. By looking at the code I see that $start and $end show where the start and end of what is to be outputted. But when I try to change those to hone in more specifically on the data it doesnt output anything. For example, I tried to just parse the body of the html. So I replaced the $start and $end with <body> and <\/body> respectively, but it doesnt output anything. So I'm thinking two things could be the solution. One- I am not properly changing the code to hone in more specifically. Two- Once the page is fetched there needs to be more done to get more specific data. If anyone could take a look at this and help me out it would be greatly appreciated because feel very very lost lol

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.