Solved

Have problem with while cycle in php script

Posted on 2008-10-08
4
326 Views
Last Modified: 2013-12-13
Okay i'm using else function in this code ,i think it causes the problem.But not sure how to fix it.The error as follows

Notice: Undefined variable: STR in C:\\www\testas.php on line 24

it creates empty .txt file instead of parsed one.
<?
error_reporting(E_ALL);
$source = $_POST['source'];
$output = $_POST['output'];
$first = $_POST['first'];
$second = $_POST['second'];
 
if((empty($source))OR(empty($output))OR(empty($first))OR(empty($second))) { $output= "NO FILE"; }  else {
 
$contents=file_get_contents($source);
 
$c1= $first;
$c2= $second;
 
$html = preg_replace("/\n/"," ",$contents);
 
preg_match_all("/(.*?)<td class=\"td2\" nowrap=\"nowrap\"><b>(.*?)<\/b><\/td>(.*?)/",$html,$tags);
 
while(list($key,$val)=each($tags[2])){
  $STR.=$c1."".$val."".$c2."\n";
}
 
$fp=fopen($output,"a+");
fwrite($fp,"$STR");
fclose($fp);
}
?>

Open in new window

0
Comment
Question by:olivest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:DerkArts
ID: 22667223
It seems that your regex is not working properly. You while loop isnt running because the list is empty.

PLease check de result of your regex en post back.   ( print_r($tags); )

By the way, foreach is much easier here:

foreach($tags[2] as $key=>$val){
  $STR.=$c1."".$val."".$c2."\n";
}

but you still need to check your regex first
0
 
LVL 3

Expert Comment

by:DerkArts
ID: 22667253
Print POST too... That way you can make sure you if else is working properly!

print_r($_POST);
0
 

Author Comment

by:olivest
ID: 22667302
Okay i've attached full source code and output.Follow up.

Notice: Undefined variable: tags in C:\www\testas.php on line 3
Array ( [MAX_FILE_SIZE] => 100000 [output] => done.txt [first] => call n [second] => >> 130.110.log [Extract] => Extract )
Warning: file_get_contents() expects parameter 1 to be string, array given in C:\www\testas.php on line 13

Notice: Undefined variable: STR in C:\www\testas.php on line 27

<?
error_reporting(E_ALL);
print_r($tags);
print_r($_POST);
$source = $_FILES['source'];
$output = $_POST['output'];
$first = $_POST['first'];
$second = $_POST['second'];
 
if((empty($source))OR(empty($output))OR(empty($first))OR(empty($second))) { $output= "NO FILE"; }  else {
$target_path = ".";
$target_path = $target_path . basename( $_FILES['source']['name']); 
$contents=file_get_contents($source);
 
$c1= $first;
$c2= $second;
 
$html = preg_replace("/\n/"," ",$contents);
 
preg_match_all("/(.*?)<td class=\"td2\" nowrap=\"nowrap\"><b>(.*?)<\/b><\/td>(.*?)/",$html,$tags);
 
foreach($tags[2] as $key=>$val){
  $STR.=$c1."".$val."".$c2."\n";
}
 
$fp=fopen($output,"a+");
fwrite($fp,"$STR");
fclose($fp);
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ip Extractor</title>
<style type="text/css">
<!--
.style1 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 10px;
	color: #999999;
}
.style2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 12px;
}
.style5 {color: #0066CC}
.style6 {
	color: #0066CC;
	font-weight: bold;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
-->
</style>
</head>
 
<body>
<form enctype="multipart/form-data" id="ipforma" name="ipforma" method="post" action="#">
  <label> <span class="style2">  <span class="style5">Please enter file to parse</span>
  <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  </span></label>
  <table width="350" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="132" class="style1" scope="row">Source File Name</th>
    <td width="218"><input name="source" type="file" class="style1" id="source" value="savas.html" hspace="0" vspace="0" border="0"/></td>
  </tr>
  <tr>
    <th scope="row"><span class="style1">Output File Name</span></th>
    <td><label>
      <input name="output" type="text" class="style1" id="output" value="done.txt" hspace="0" vspace="0" border="0" />
    </label></td>
  </tr>
  <tr>
    <th class="style1" scope="row">First Ln</th>
    <td><label>
      <input name="first" type="text" class="style1" id="first" value="call n " hspace="0" vspace="0" border="0" />
    </label></td>
  </tr>
  <tr>
    <th class="style1" scope="row">Second Ln</th>
    <td><label>
      <input name="second" type="text" class="style1" id="second" value=" &gt;&gt; 130.110.log" hspace="0" vspace="0" border="0"/>
    </label></td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td><label>
      <input name="Extract" type="submit" class="style2" id="Extract" value="Extract" />
    </label></td>
  </tr>
</table>
  <p class="style6">Output file here <?php echo('<a href="'.$output.'">'.$output.'</a>'); ?> </p>
<p><br />
</p>
</form>
</body>
</html>

Open in new window

0
 

Accepted Solution

by:
olivest earned 0 total points
ID: 22667779
Okay Don't matter FIXED it attached the source code if any one needs it
<?
//***** IP Address Extractor *****
//****Credits to nFinite[GPU]*****
//*** nfinitegpu@hotmail.com *****
 
$source = $_FILES['source']["name"];;
$output = $_POST['output'];
$first = $_POST['first'];
$second = $_POST['second'];
if((empty($source))OR(empty($output))OR(empty($first))OR(empty($second))) { $output=""; }  else {
 
$target_path = "C:/TestSrv/AppServ/www/";
$target_path = $target_path . basename( $_FILES['source']['name']); 
move_uploaded_file($_FILES["source"]["tmp_name"], $target_path);
$srcfl = $_FILES["source"]["name"];
$contents=file_get_contents($srcfl);
 
$c1= $first;
$c2= $second;
 
$html = preg_replace("/\n/"," ",$contents);
 
preg_match_all('/(.*?)(\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' .
     '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' .
     '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.' .
     '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)(.*?)/',
     $html,$tags);
 
while(list($key,$val)=each($tags[2])){
  $STR.=$c1."".$val."".$c2."\n";
}
 
$fp=fopen($output,"a+");
fwrite($fp,"$STR");
fclose($fp);
}
?>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IP Extractor</title>
<style type="text/css">
<!--
.style1 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 10px;
	color: #999999;
}
.style2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 12px;
}
.style5 {color: #0066CC}
.style6 {
	color: #0066CC;
	font-weight: bold;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
-->
</style>
</head>
 
<body>
<form enctype="multipart/form-data" id="ipforma" name="ipforma" method="post" action="#">
  <label> <span class="style2">  <span class="style5">Please enter file to parse</span>
  <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  </span></label>
  <table width="350" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th width="132" class="style1" scope="row">Source File Name</th>
    <td width="218"><input name="source" type="file" class="style1" id="source" value="savas.html" hspace="0" vspace="0" border="0"/></td>
  </tr>
  <tr>
    <th scope="row"><span class="style1">Output File Name</span></th>
    <td><label>
      <input name="output" type="text" class="style1" id="output" value="done.txt" hspace="0" vspace="0" border="0" />
    </label></td>
  </tr>
  <tr>
    <th class="style1" scope="row">First Ln</th>
    <td><label>
      <input name="first" type="text" class="style1" id="first" value="call n " hspace="0" vspace="0" border="0" />
    </label></td>
  </tr>
  <tr>
    <th class="style1" scope="row">Second Ln</th>
    <td><label>
      <input name="second" type="text" class="style1" id="second" value=" &gt;&gt; 130.110.log" hspace="0" vspace="0" border="0"/>
    </label></td>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td><label>
      <input name="Extract" type="submit" class="style2" id="Extract" value="Extract" />
    </label></td>
  </tr>
</table>
 <?php if((empty($output))) 
 { 
  echo('<p class="style6">Status: Idle</p>'); 
 }
 else {
 
 echo('<p class="style6">Status: File Parsed <a href="'.$output.'">'.$output.'</a></p>');
 } ?>
<p><br />
</p>
 
 
</form>
</body>
</html>

Open in new window

0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question