Perl to PHP conversion?
I need some help converting some Perl script to PHP - any help would be very much appreciated....
I have attached the perl file
Attachment | Size |
---|---|
perl.txt | 1.79 KB |
I need some help converting some Perl script to PHP - any help would be very much appreciated....
I have attached the perl file
Attachment | Size |
---|---|
perl.txt | 1.79 KB |
Greg K posted this at 18:03 — 30th March 2004.
He has: 2,145 posts
Joined: Nov 2003
I didn't test any of the following, but it should work. There are a few notes though:
1. I'm assuming this page will be called from a form using the POST method.
2. The preg_match may need to be tested, I just went off of the docs for php on how to use it. I gave the URL in a comment in the code
3. The number_format just uses the default settings. I provided a link to its documentation in case you need to adjust how it formats
4. The htmlentities is used to convert any special HTML characters over so they display as characters, and not get interpreted by the browser.
Here is the code, hope it helps!
-Greg
<?php
function err($area,$value)
{
print \"Error in \" . $area . \" field - \" . $value;
print \"</body></html>\n\";
exit;
}
// Get files from the form being submitted??
$price = $_POST['PRICE'];
$bedrooms = $_POST['BEDROOMS'];
$comments = $_POST['COMMENTS'];
$location = $_POST['LOCATION'];
$prototype = $_POST['PROPTYPE'];
$name = $_POST['NAME'];
$telephone = $_POST['TELEPHONE'];
$email = $_POST['EMAIL'];
// Note, I am not good with doing expression matching, so you may need to
// check <a href="http://www.php.net/manual/en/function.preg-match.php" title="http://www.php.net/manual/en/function.preg-match.php">http://www.php.net/manual/en/function.preg-match.php</a> for details
// In the docs, it said it is PERL compatable expression matching
if (!preg_match(\"/\A\d+\Z/\",$price) || $price > 1E6 || $price < 0)
err(\"PRICE\",$price);
if (!preg_match(\"m/\A\d+\Z/\",$bedrooms) || $bedrooms > 10 || $bedrooms <1)
err(\"BEDROOMS\",$bedrooms);
if (!preg_match(\"m/\w|/\",$comments))
err(\"COMMENTS\",$comments};
if (!preg_match(\"m/London|Manchester|Coventry|Leeds/i\",$location))
err(\"LOCATION\",$location);
if (!preg_match(\"m/Freehold|Leasehold/i\",$prototype))
err(\"PROTOTYPE\",$prototype);
if (!preg_match(\"/\A[a-z]+\Z/i\",$name))
err(\"NAME\",$name);
if (!preg_match(\"m/\A\d+\Z/i\",$telephone))
err(\"TELEPHONE\",$telephone);
if (!preg_match(\"/\A[a-z]+\@[a-z]+\Z/i)\",$email))
err(\"EMAIL\",$email);
// Changing the prototype to be just the first letter
$prototype = substr($prototype,0,1);
print \"<h1>Search Matched</h1>\n\";
$query = \"SELECT price,bedrooms,comments FROM houses \" .
\"WHERE \" .
\" bedrooms >= \" . $bedrooms . \" AND \" .
\" price <= \" . $price . \" AND \" .
\" location = \" . $location . \" AND \" .
\" prototype = \" . $prototype;
$dbHost = \"possum\";
$dbName = \"staffkeithy\";
$dbUser = \"\"; // your user name
$dbPass = \"\"; // your password
$dbConn = @mysql_connect($dbHost, $dbUser, $dbPass)
or die(\"Can't connect to database\");
if ($dbName != \"\" && !@mysql_select_db($dbName))
die (\"The site database in unavailable\");
$dbResult = mysql_query($query,$dbConn)
or die(\"Query Error: \" . mysql_error());
if (mysql_num_rows($dbResult) > 0)
{
print \"<table border=\\"1\\">\n\";
print \"<tr><td>Price</td><td>Bedrooms</td><td>Comments</td></tr>\n\";
while($thisRow = mysql_fetch_array($dbResult))
{
print \"<tr>\";
// see <a href="http://www.php.net/numberformat" title="http://www.php.net/numberformat">http://www.php.net/numberformat</a> for settings for fomatting #'s
print \"<td>£\" . number_format($thisRow['price']) . \"</td>\";
print \"<td>\" . $thisRow['bedrooms'] . \"</td>\";
// see <a href="http://www.php.net/manual/en/function.htmlentities.php" title="http://www.php.net/manual/en/function.htmlentities.php">http://www.php.net/manual/en/function.htmlentities.php</a> for
// info on the htmlentities() function.
print \"<td>\" . htmlentities($thisRow['comments']) . \"</td>\";
print \"</tr>\n\";
}
print \"</table>\n\";
}
else
print \"<p>Sorry, no results were found.</p>\n\";
mysql_close($dbConn);
?>
unitunes posted this at 18:08 — 30th March 2004.
They have: 18 posts
Joined: Mar 2004
Greg - thanks for that i'll try it out
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.