mbabli Posted August 30, 2006 Share Posted August 30, 2006 Hello, If I have a javascript file with the following: var username="test"; var pagesVisisted=new Array( "http://www.yahoo.com", "http://www.msn.com", "http://www.php.net" ); var lastHit="0608301502"; And I want ONLY the stuff in the pagesVisited Array, how can I do that? Any help would be appreciated. Quote Link to comment Share on other sites More sharing options...
mbabli Posted August 30, 2006 Author Share Posted August 30, 2006 got it, $fp = @file(myfile.js); for($i=0; $i < count($fp); $i++) { if(strstr($fp[$i], "visitedPages")) { while($fp[$i++] != strpos($fp[$i],");")) echo "<tr><td>".$fp[$i]."</td></tr>"; }}I still need to strip the " " from around the string, I tried using preg_match("/\"(\w+)\";/", $fp[$i]) with no success. Is there a better way to do this? Quote Link to comment Share on other sites More sharing options...
effigy Posted August 31, 2006 Share Posted August 31, 2006 [code]<pre><?php $data = <<<DATA var username="test"; var pagesVisisted=new Array( "http://www.yahoo.com", "http://www.msn.com", "http://www.php.net" ); var lastHit="0608301502";DATA; $data_array = explode("\n", $data); $result = array(); foreach ($data_array as $line) { if (preg_match('/^\s*"(.+?)"/', $line, $matches)) { $result[] = $matches[1]; } } print_r($result);?></pre>[/code] Quote Link to comment Share on other sites More sharing options...
mbabli Posted August 31, 2006 Author Share Posted August 31, 2006 Hello, Thanks for the reply. Unfortunately, the regular expression didn't cut it for me. It only displayes elements starting from the 2nd, 3rd..etc. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 31, 2006 Share Posted August 31, 2006 This is the result I am getting:[code]Array( [0] => http://www.yahoo.com [1] => http://www.msn.com [2] => http://www.php.net)[/code]Is there more content in your JS file? Quote Link to comment Share on other sites More sharing options...
mbabli Posted August 31, 2006 Author Share Posted August 31, 2006 The JS file is user dependent, so, maybe just that or maybe a few thousand lines. + ~10 more variable arrays. My workaround was to simply use str_replace twice, once to filter the first entry and the 2nd to filter the rest of the array entries. Here is what I have so far.<pre> $fp = @file('visitedPages.js'); for($i=0; $i < count($fp); $i++) { if(strstr($fp[$i], "visitedPages")) { while($fp[$i++] != strpos($fp[$i],");")) { $result = str_replace('"',' ',$fp[$i]); echo "<tr><td><a class=\"linkList\" href=\"#\">".str_replace(',',' ', $result)."</a></td></tr>"; } } } It does the job for now. But, if there is a better way to do this, I will be in your debt.Thanks for the followup.</pre> Quote Link to comment Share on other sites More sharing options...
rea|and Posted August 31, 2006 Share Posted August 31, 2006 I'm not sure it is what you want, but, first of all, I guess is better to use file_get_contents() instead of file(). Try this one :[CODE]$js = file_get_contents('visitedPages.js') ;preg_match('/visitedPages[^\(]+\([^)]+\)/',$js,$mt) ;preg_match_all('/"([^"]+)"/',$mt[0],$mh) ;echo '<pre>';print_r($mh[1]);[/CODE] I've used [B]visitedPages[/B] as js array name. 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.