note 49437 added to function.strlen

From: Date: Thu, 27 Jan 2005 21:39:32 +0000
Subject: note 49437 added to function.strlen
Groups: php.notes 
Request: Send a blank email to php-notes+get-84091@lists.php.net to get a copy of this message
I found that I needed something a little beyond array pushing and popping so I could share a stack between calls to a script. I wrote these quick functions, and while they are not all that efficient, they are certainly better than hitting a database server to manage a stack between script calls. Anyway, here we go. function FilePush($filename, $thingToPush, $terminator="\n") { $oldStuff = file_get_contents($filename); //Get the old file $result = file_put_contents ( $filename, $thingToPush . $terminator . $oldStuff ); //add the new stuff to the old and write it return $result; //return whatever file_put_contents says } function FilePop($filename, $terminator="\n") { $oldStuff = file_get_contents($filename); //get those file contents $pos = strpos($oldStuff, $terminator); // find our first terminator if ($pos == false) { //No terminator detected. Is there one item or no items? if (strlen($oldStuff) < 1) { // There is nothing! $result = false; } else { //There is only one thing! $result = $oldStuff; //store that thing file_put_contents ( $filename, ''); //pop it out of file } } else { //There are many things, and we found the first one $result = substr($oldStuff, 0, $pos); //get the first thing file_put_contents ( $filename, substr($oldStuff, ($pos + strlen($terminator))) ); //re-put everything but the popped item in the file. } return $result; //return whatever happened } ---- Manual Page -- http://www.php.net/manual/en/function.strlen.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49437 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49437&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49437&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49437&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49437&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49437&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49437&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#84091) next »