PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Feb 06, 2012 5:37 pm
OK, I wasn't sure if you wanted to return partial matches or what you wanted to do when a match was found. Are you looking for something like this?
Code: Select all
<?php
if (!empty($_POST))
{
$serial = $_POST['serial_box_div']; //loads the var value
if (preg_match('/\d{5}/', $serial))
{
$file = @fopen("assitenciatecnica.txt", "r"); //read file
if ($file)
{ //validate file
while (!feof($file))
{
$line = fgets($file, 1024); //read line
if (preg_match('/^' . $serial . '/', $line))
{
$data = explode(' ', $line);
$id = rtrim($data[1], ';');
echo "<div id=\"id_{$id}\">Div id_{$id} created.</div>";
}
}
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Debug</title>
</head>
<body>
<form action="" method="post"><input name="serial_box_div" type="text" /> <input name="" type="submit" value="enviar"/></form>
</body>
</html>
Colucas
Forum Newbie
Posts: 10 Joined: Mon Feb 06, 2012 6:05 am
Post
by Colucas » Mon Feb 06, 2012 5:49 pm
hmm nice! thats it! rtrim gets the next character after the ; ? That's exactly what I was looking for!! ^^
Can you please also add another behaviour? If the serial isn't found, then a error div would appear? Tried else's along the code but none worked, guess that's not how its done!
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Feb 06, 2012 5:51 pm
Code: Select all
<?php
if (!empty($_POST))
{
$serial = $_POST['serial_box_div']; //loads the var value
if (preg_match('/\d{5}/', $serial))
{
$file = @fopen("assitenciatecnica.txt", "r"); //read file
if ($file)
{ //validate file
while (!feof($file))
{
$line = fgets($file, 1024); //read line
if (preg_match('/^' . $serial . '/', $line))
{
$data = explode(' ', $line);
$id = rtrim($data[1], ';');
echo "<div id=\"id_{$id}\">Div id_{$id} created.</div>";
}
}
}
}
else
{
echo "Serial must be 5 digits.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Debug</title>
</head>
<body>
<form action="" method="post"><input name="serial_box_div" type="text" /> <input name="" type="submit" value="enviar"/></form>
</body>
</html>