tobykw13 Posted October 5, 2007 Share Posted October 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/71883-having-trouble-parsing-data-from-page/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.