Jump to content


Photo

Extracting multiple lines after a certain string


  • Please log in to reply
6 replies to this topic

#1 mbabli

mbabli
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 30 August 2006 - 10:28 PM

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.


 

#2 mbabli

mbabli
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 30 August 2006 - 11:13 PM

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?



#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 31 August 2006 - 04:11 AM

<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>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 mbabli

mbabli
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 31 August 2006 - 02:31 PM

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.


#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 31 August 2006 - 04:56 PM

This is the result I am getting:

Array
(
    [0] => http://www.yahoo.com
    [1] => http://www.msn.com
    [2] => http://www.php.net
)

Is there more content in your JS file?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#6 mbabli

mbabli
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 31 August 2006 - 05:54 PM

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>


#7 rea|and

rea|and
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 31 August 2006 - 06:44 PM

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 :
$js = file_get_contents('visitedPages.js') ;


preg_match('/visitedPages[^\(]+\([^)]+\)/',$js,$mt) ;
preg_match_all('/"([^"]+)"/',$mt[0],$mh) ;

echo '<pre>';
print_r($mh[1]);

I've used visitedPages as js array name.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users