Jump to content

Can this code be made more friendly according to the latest php versions


Recommended Posts

Hello,

 

I have found this fantastic code here:

http://www.funphp.com/?p=33#comment-62497

 

But I am wondering if all the functions are still working fine with the latest php version.

 

Also I have 2 questions about this script:

 

1)I am not understanding the "$response='NEW:1", where does the "NEW:1" comes from, I have never  seen that before.

 

2)If I wanted to change the function:

$output = ereg_replace("[ \t\n\r]","",$input)."\0";

 

to

preg_replace

 

Would this works?

$output = preg_replace("[ \t\n\r]","",$input)."\0";

 

 

 

 
 
Thank you
 
Ben
/*********function to check new order******************/
function get_new_order()
{
$con=mysql_connect(HOST, USERNAME, PASSWORD);
mysql_select_db(DATABASE, $con);
$sql="select OrderId from customer_order where order_Status='0' "; //0 for new order
$query=mysql_query($sql,$con);
if(mysql_num_rows( $query)>0)
{
return true;
}
else return false;
}
/*************************************/
/********Socket Server*********************/
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 6789;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0); // 0 for SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, 0, $port) or die('Could not bind to address'); //0 for localhost
// Start listening for connections
socket_listen($sock);
//loop and listen
while (true) {
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024000 bytes
$input = socket_read($client, 1024000);
// Strip all white spaces from input
$output = ereg_replace("[ \t\n\r]","",$input)."\0";
$message=explode('=',$output);
if(count($message)==2)
{
if(get_new_order()) $response='NEW:1';
else $response='NEW:0';
}
else $response='NEW:0';
// Display output back to client
socket_write($client, $response);
socket_close($client);
}
// Close the master sockets
socket_close($sock);

 

mysql_* functions are deprecated, use pdo or mysqli

 

 

The code on that page converted to html entities

Go through all the codes and replace any html entities ( &characters; ) to their proper characters

http://dev.w3.org/html5/html-author/charref

 

This line:

if(mysql_num_rows( $query)>0)
{

 

should have been:

if(mysql_num_rows($query) > 0)
{

 

$response='NEW:1' is what that person is saving the response as.

 

ereg_replace() to preg_replace() conversion is a matter of adding a delimeter and also escape anything required with a backslash \

http://php.net/manual/en/reference.pcre.pattern.posix.php

 

Some alternatives:

added i for a pattern modifier for case insensitive because POSIX regex was case-insensitive and a + for multiples

$output = preg_replace("/[ \t\n\r]+/i","",$input)."\0";

$output = preg_replace("~[\s\t\n\r]+~ i","",$input)."\0";

 

$output = preg_replace('/\s+/', '', trim($input))."\0";

$output = str_replace(array("\r\n","\r","\n","\t"," "),"",trim($input))."\0";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.