PDA

View Full Version : Resolved PHP RegEx Incorrect



bluewalrus
06-21-2010, 02:27 AM
1) Describe Work Request (be as detailed as possible): I can't quite get this to work quite right I know I'm writing a new line or tab incorrectly.

I'm using 3 txt files and 3 php files...

2bedone.txt:



<table border="1">
<tr>
<td style="font-weight:bold;">Pending Task</td>
<td style="font-weight:bold;">Date Requested</td>
<td style="font-weight:bold;" id="user">Requestor</td>
</tr>
</table>

done.txt:



<table border="1">
<tr>
<td style="font-weight:bold;">Task Completed</td>
<td style="font-weight:bold;">Date Requested</td>
<td style="font-weight:bold;">Date Completed</td>
</tr>
</table>


r_count.txt:



0



performed.php:



<?php
if (isset($_GET['id_is'])) {
$id_on = $_GET['id_is'];
$pending_file = '2bedone.txt';
$pending_files = file_get_contents($pending_file);
$pattern = '/<tr id="' . $id_on . '">\n\t\t<td>(.*)<\/td>\n\t\t<td>(.*)<\/td>\n\t\t<td class="user">(.*)<\/td>\n\t<\/tr>\n/';
$replacement = "";
preg_match($pattern, $pending_files, $matches);
echo "\nMatches\n" . $matches[0];
$move_over = preg_replace($pattern, $replacement, $pending_files);
echo "\nMove Over\n" . $move_over;
file_put_contents($pending_file, $move_over);
//Write to done file
$hour = date("g") + 1;
$date = date("n/d/y $hour:i:s a");
$complete_file = 'done.txt';
$complete_files = file_get_contents($complete_file);
$second_pattern = '/<\/td>\n\t<\/tr>\n<\/table>/';
$second_replacement = "</td>\n\t</tr>\n$matches[0]</table>";
echo "\n2nd Pattern\n" . $second_replacement;
$holder = preg_replace($second_pattern, $second_replacement, $complete_files);
$third_pattern = '/<tr id="' . $id_on .'">\n\t\t<td>(.*)<\/td>\n\t\t<td>(.*)<\/td>\n\t\t<td class="user">\d+<\/td>\n\t<\/tr>/';
$third_replacement = "<tr id=\"$id_on\">\n\t\t<td>$1</td>\n\t\t<td>$2</td>\n\t\t<td>$date</td>\n\t</tr>";
$completed_content = preg_replace($third_pattern, $third_replacement, $holder);
file_put_contents($complete_file, $completed_content);
// header("Location: http://NotLive.net/taskmanager/index.php?submit_change=2");
} else {
header("HTTP/1.0 404 Not Found");
}
?>


index.php:



<!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>Changes Tasks</title>
</head>
<body>
<?php
if (isset($_GET['submit_change']) && $_GET['submit_change'] == "1") {
?>
<form action="process.php" method="post">
Task:<br /><textarea name="pending"></textarea><br />
Requestor:<select name="requestor">
<option value="0">User/IT</option>
<option value="1">Video Request</option>
</select><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
<?php
}
if (!isset($_GET['submit_change']) || $_GET['submit_change'] !=2) {
?>
<h1>Pending Tasks</h1>
<?php
$pending_tasks = file_get_contents('2bedone.txt');
$pending_tasks = str_replace('class="user">0</td>', "class=\"user\">User Request</td>", $pending_tasks);
$pending_tasks = str_replace('class="user">1</td>', "class=\"user\">Video Request</td>", $pending_tasks);
echo $pending_tasks . "\n";
?>
<h1>Completed Tasks</h1>
<?php
$complete_tasks = file_get_contents('done.txt');
echo $complete_tasks . "\n";
}
if (isset($_GET['submit_change']) && $_GET['submit_change'] == "2") {
if (!isset($_POST['p_word'])) {
?>
<form action="?submit_change=2" method="post">
<input name="p_word" type="password" />
<input type="submit" value="Submit" />
</form>
<?php
}
if (isset($_POST['p_word']) && $_POST['p_word'] == "password" ) {
$pending_tasks = file_get_contents('2bedone.txt');
$pattern = '/<tr id="(\d+)">\n\t\t<td>(.*)<\/td>\n\t\t<td>(.*)<\/td>\n\t\t<td class="user">(.*)<\/td>\n\t<\/tr>/';
$replacement = "<tr id=\"$1\">\n\t\t<td><a href=\"http://NotLive.net/taskmanager/performed.php?id_is=$1\">$2</a></td>\n\t\t<td><a href=\"http://NotLive.net/taskmanager/performed.php?id_is=$1\">$3</a></td>\n\t\t<td class=\"user\"><a href=\"http://NotLive.net/taskmanager/performed.php?id_is=$1\">$4</a></td>\n\t</tr>";
$list_changes = preg_replace($pattern, $replacement, $pending_tasks);
echo $list_changes;
}
?>
</body>
</html>
<?php
}
?>


From index.php there are 3 pages...

1. index.php?submit_changes=1 this loads a form to submit a new request
2. index.php?submit_changes=2 this loads a securish form that once verified the user can send a task from the 2bedone to the done.txt
3. index.php this shows all tasks pending and all tasks done.

Please let me know if there are any other questions. This works for adding a task and swapping just not together, the txt files must be altered for that.

2) Payment methods you support (ie: Paypal, 2checkout etc):

Paypal

Not sure how much time this will take someone, I'd imagine not much for someone with a good understanding though...

james438
06-22-2010, 12:45 AM
Hi, hope you don't mind me poking my nose in here, but your problem sounds interesting.

I was looking over your post and the codes. Could you post the code for process.php?

bluewalrus
06-22-2010, 01:49 PM
I actually just found the problem, it was pretty simple I just needed to start the searches with a tab as well because I wasn't replacing it otherwise.

I.E the patterns should be


$pattern = '/\t<tr id="' . $id_on . '">\n\t\t<td>(.*)<\/td>\n\t\t<td>(.*)<\/td>\n\t\t<td class="user">(.*)<\/td>\n\t<\/tr>\n/';

instead of


$pattern = '/<tr id="' . $id_on . '">\n\t\t<td>(.*)<\/td>\n\t\t<td>(.*)<\/td>\n\t\t<td class="user">(.*)<\/td>\n\t<\/tr>\n/';

traq
06-22-2010, 07:21 PM
So, out of curiosity, how much are you going to pay yourself for the solution? :p