Jump to content

jaybo

Members
  • Posts

    42
  • Joined

  • Last visited

jaybo's Achievements

Member

Member (2/5)

0

Reputation

3

Community Answers

  1. Update - So I filtered the array using array_filter() to remove the empty array sets throughout the results set. Then I found that I could select the code I needed by testing on regexr.com with the expression 'gpononu.*\n.*?$' in a preg_match_all() function. On regexr.com the flags used are /gm but when I try to run in my code I am unable to get anything to print to the browser. // $resultOutput is the ARRAY FROM RESULT //remove empty array sets $resultOutput1 = array_filter($resultOutput); $resultOutput2 = preg_match_all('gpononu.*\n.*?$', $resultOutput1); echo '<pre>'; echo "\n------------------------>>>>>>>\n"; print_r($resultOutput2); echo "\n------------------------>>>>>>>\n"; echo '</pre>'; I have tried to add /gm to the regex but it still doesn't work. Any ideas?
  2. Hi, I am outputting an array and splitting up the result sets using regular expression... I need to select the range of data that will select the data from gpononu on one line up until the end of the first IP address on another line. example array: [0] => bridge show [1] => [2] => [3] => Orig [4] => [5] => Type VLAN/SLAN VLAN/SLAN Physical Bridge St Table Data [6] => [7] => --------------------------------------------------------------------------------------------------------------------- [8] => [9] => dwn-p2p Tagged 222 1/1/1/1/gpononu 1-1-1-257-gponport-222/bridge UP D 00:02:71:db:bb:eb [10] => [11] => D 216.19.250.121 [12] => [13] => dwn-p2p Tagged 222 1/1/1/2/gpononu 1-1-1-258-gponport-222/bridge UP D 00:02:71:db:bb:df [14] => [15] => D 216.19.250.138 I know that I can select the line with gpononu by using: gpononu and I know I can find the IP's using: \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b But not sure how to put that together to start and end the pattern match.
  3. Thank you Barand - that helped! Appears to work now.
  4. I have some code that is using Ajax long polling, connecting to a mysql DB and then outputting the current data upon a change to the table. I am implementing this page so that while 2 or more people are on the same admin page at the same time they will see any changes the other(s) makes real time. Here is the sql: CREATE TABLE `customers` ( `id` int(55) NOT NULL, `lastModified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `firstName` int(55) NOT NULL, `lastName` int(55) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; ALTER TABLE `customers` ADD PRIMARY KEY (`id`); FILE 1: cusDetLongPoll.php <?php ini_set('display_errors', 1); error_reporting(E_ALL); ?> <!-- (A) HTML SCOREBOARD --> <div id="sTime"></div> <div id="sBoard"> <div id="sFirst"></div> <div id="sLast"></div> <div id="First">First</div> <div id="Last">Last</div> </div> <script> // (B) LAST UPDATED TIMESTAMP var last = 0; // (C) AJAX LONG POLL function poll () { // (C1) FORM DATA let data = new FormData(); data.append("last", last); console.log("Fetch run", last); // (C2) FETCH UPDATE ON SERVER RESPONSE fetch("modules/customer/cusDetLongPoll2.php", { method:"POST", body:data }) .then(res => res.json()) .then(data => { //console.log(data); // (C2-1) UPDATE HTML DISPLAY document.getElementById("sTime").innerHTML = data.lastModified; document.getElementById("sFirst").innerHTML = data.firstName; document.getElementById("sAway").innerHTML = data.lastName; // (C2-2) NEXT ROUND last = data.unix; poll(); }) // (C3) CATCH ERROR - LOOP ON TIMEOUT .catch(err => poll()); } // (D) GO! window.onload = poll; </script> FILE 2: cusDetLongPoll2.php <?php ini_set('display_errors', 1); error_reporting(E_ALL); class CustomerDetails { // (A) CONSTRUCTOR - CONNECT TO DATABASE protected $pdo = null; protected $stmt = null; function __construct () { try { $this->pdo = new PDO( "mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET, DB_USER, DB_PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); } catch (Exception $ex) { exit($ex->getMessage()); }} // (B) DESTRUCTOR - CLOSE CONNECTION function __destruct () { if ($this->stmt !== null) { $this->stmt = null; } if ($this->pdo !== null) { $this->pdo = null; } } // (C) GET LATEST CUSTOMER DETAILS function getCusDetails() { $this->stmt = $this->pdo->prepare( "SELECT *, UNIX_TIMESTAMP(`lastModified`) AS `unix` FROM `customers` WHERE id = 1" ); $this->stmt->execute(); return $this->stmt->fetch(); } } // (D) DATABASE SETTINGS - CHANGE THESE TO YOUR OWN! define("DB_HOST", "localhost"); define("DB_NAME", "NAME"); define("DB_CHARSET", "utf8"); define("DB_USER", "USER"); define("DB_PASSWORD", "PASSWORD"); // (E) CHECK FOR CUSTOMER UPDATES // ******** THIS IF STATEMENT IS NOT WORKING - WHEN REMOVED WE CAN PRINT_R THE CUSTOMER DETAILS AS ARRAY. WHEN USED THE IF STATEMENT RETURNS NO DATA. ********* if (isset($_POST["last"])) { // (E1) SET TIME LIMIT set_time_limit(30); // set an appropriate time limit ignore_user_abort(false); // stop when long polling breaks // (E2) LOOP UNTIL THERE ARE UPDATES OR TIMEOUT $_DETAILS = new CustomerDetails(); //print_r($_DETAILS->getCusDetails()); while (true) { $details = $_DETAILS->getCusDetails(); if (isset($details["unix"]) && $details["unix"] > $_POST["last"]) { echo json_encode($details); break; } sleep(1); // short pause to not break server } } From my testing I have found that in the code above at > if (isset($_POST["last"])) { ... When this is taken out and I run the PRINT_R on the $_DETAILS->getCusDetails(); I see the output on the screen. When I add in the if statement then I do not get any output or data at all. I ran this code the first time and it worked, changed the data and timestamp in the DB and it auto updated. But then my browser ran a refresh through my code and it hasn't worked since. May be totally unrelated but this was the order of events. Any help appreciated!!
  5. UPDATE: Did not find a solution to the exact issue - the newer telnet class was also not connecting for me. I have, however, been able to get a perl connection using Net::Telnet component, then run a short php script to output ALL results. This happened within 3 seconds for 900+ results so all in all a good replacement.
  6. regex is matching and adding the testing of $line still produces the timeout. I am trying to echo out each line as it is read also but not getting any output at all currently. Looking at the http.conf file for apache to see if I can change the timeout for the server and see if this helps.
  7. Update - new class not connecting for me, still looking at that one. Current class I am also trying to modify still produces a Gateway Timeout with the newly modified code addition; //edited code to attempt to read the eof to allow all data to pass before close of socket function GetResponse(&$r) { $r = ''; do { $line = fgets($this->fp); $r .= $line; } while (!preg_match('/\d{1,4} Bridge Interfaces displayed/', $line)); } timeout is occurring at exactly 1min. **From tailing the log file I am also seeing that the Fatal Error is due to the Maximum execution time of 300 seconds exceeded (used php_ini to set this).
  8. Thank you. Will work on this and come back with updates/questions!
  9. So in looking at making the current class work through modification: The last line upon completing the output (identifier) is always ‘[1-4 digits number*] Bridge Interfaces displayed’ for the tasks I am running. So I am looking at where to state this as the EOF prompt in the script? In addition when connecting via telnet from the terminal it takes between 10sec-45sec+ depending on the server chassis I am connecting to. Not sure if this would mean I need to tweak the socket_timeout to allow for this?
  10. Thanks Kicken. Currently struggling to connect to telnet with the new class. Will invest some time then come back with an update.
  11. Thank you mac_gyver for that useful input. I tried to switch out while ($s['unread_bytes']); with while (! feof($s)); but this causes an internal error. Not too sure where to put the feof().
  12. Still also trying to debug the other code with PHPTelnet class library. I have a bunch of code that I use with it and would like to find the answer to that too!
  13. Thanks for the additional option here - I'll look to test with that library.
×
×
  • 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.