jk11uk Posted December 20, 2007 Share Posted December 20, 2007 hi guys i'd like to make a script that goes through a site (has dynamic URLs) and reads a certain line of text from each page and extracts the second number in from it. my question is: how do you get a php script to access a URL and extract the source code of the page and store it into a varialbe? secondly: how would you then extract a certain sentence from the page if you know what the starting few words will always be and the ending few words? so if i know it will be 'word one word two UNKNOWN WORDS end word'. thanks so much for any assistance some genious can offer ! Quote Link to comment Share on other sites More sharing options...
chigley Posted December 20, 2007 Share Posted December 20, 2007 <?php function textbetweenarray($s1,$s2,$s){ $myarray=array(); $s1=strtolower($s1); $s2=strtolower($s2); $L1=strlen($s1); $L2=strlen($s2); $scheck=strtolower($s); do{ $pos1 = strpos($scheck,$s1); if($pos1!==false){ $pos2 = strpos(substr($scheck,$pos1+$L1),$s2); if($pos2!==false){ $myarray[]=substr($s,$pos1+$L1,$pos2); $s=substr($s,$pos1+$L1+$pos2+$L2); $scheck=strtolower($s); } } } while (($pos1!==false)and($pos2!==false)); return $myarray; } $code = file_get_contents("http://www.site.com"); list($words) = textbetweenarray("word one word two ", " end word", $code); echo $words; ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 20, 2007 Share Posted December 20, 2007 Check out CURL and regular expressions Quote Link to comment Share on other sites More sharing options...
chigley Posted December 20, 2007 Share Posted December 20, 2007 Check out CURL and regular expressions Who needs regex when you have the beauty of a function above! Quote Link to comment Share on other sites More sharing options...
Jessica Posted December 20, 2007 Share Posted December 20, 2007 Yeah... Quote Link to comment Share on other sites More sharing options...
chigley Posted December 20, 2007 Share Posted December 20, 2007 lol! Messy I know, but does the job Quote Link to comment Share on other sites More sharing options...
jk11uk Posted January 31, 2008 Author Share Posted January 31, 2008 thanks a lot, it works ish. The only problem is that when the value it returns has a comma or fullstop in it, it doesn't return anything i am using this to enter the found string into a database and the database gets updated correctly with the data for any string without this punctuation. any ideas on how to fix it? eg. this will work - string found: dfgdfgsdf but this wont: 123,44 nor will this: dsfsd,32 thanks again in advance :) Quote Link to comment Share on other sites More sharing options...
jk11uk Posted January 31, 2008 Author Share Posted January 31, 2008 Any ideas welcome. Please help!! Quote Link to comment Share on other sites More sharing options...
tibberous Posted January 31, 2008 Share Posted January 31, 2008 Yeah - I've done this tons of times, it all comes down to regex. It's easier if you use nongreedy regex - you want to add the modifiers i and u outside your expression, then it will be case insensitive and nongreedy. Quote Link to comment Share on other sites More sharing options...
jk11uk Posted January 31, 2008 Author Share Posted January 31, 2008 modifyers i and u? Sorry, i have no experience with regex or this type of coding. Is there any chance you could help me using the code above? This code half works, so hopefully there is a small change that can be made to make it take values even if there is a comma or fullstop? thanks Quote Link to comment Share on other sites More sharing options...
jk11uk Posted February 1, 2008 Author Share Posted February 1, 2008 pleaaaaasssssseeeee help! Quote Link to comment 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.