Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Have problem with while cycle in php script

Posted on 2008-10-08
4
Medium Priority
?
345 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

783 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