the last method i need to check the next token and every token till they stop being a letter char a-zA-Z… good luck even i couldn’t do it and i wrote this code…
<?php
class Scanner
{
// the current token
private $token;
// the evtual token type
private $token_type;
// length of string source;
private $length;
// position of current reader context;
private $pos;
// constrctor: accepts current file name and gets contents as string
public function __construct($source)
{
if(file_exists($source) && is_file($source))
{
return $this->fh = file_get_contents($source);
}
return(false);
}
// parses the string for string operations and toenizing.
public function Parse()
{
$i;
for($i = 0; $i < $this->length = strlen($this->fh); $i++)
{
$this->char = substr($this->fh , $i , 1);
return $this->token = $this->char;
}
}
// Tokenize: suppose to turn current string into known
// token type.
public function Tokenize()
{
if($this->isStringChar($this->token) == true)
{
$this->manageStringChar($this->token);
}
}
// check if current token is a string char
public function isStringChar($char)
{
if(preg_match('/[a-zA-Z]/' , $char))
{
return(true);
}
return(false);
}
// this function is suppose to match the nextchar
// and if the next char is a string char get chars till
// they stop being string chars;
public function manageStringChar($chars)
{
//get all chars aslong as the are a char between a-z A-Z
// truely stuck here.....
while($chars++ && false != preg_match('/[a-zA-Z]/' , $chars))
{
echo $chars;
}
}
}
class FileCharacterIterator implements Iterator
{
protected
$handle;
protected
$key;
public function __construct($file){
if(false === is_readable($file)){
throw new Exception($file . ' is unreadable');
}
$this->handle = fopen($file, 'r');
}
public function current(){
return fread($this->handle, 1);
}
public function key(){
return $this->key;
}
public function next(){
$this->key++;
}
public function rewind(){
$this->key = 0;
rewind($this->handle);
}
public function valid(){
return false === feof($this->handle);
}
}
class AlphaCharFilter extends FilterIterator
{
public function accept(){
return 1 === preg_match('~[a-z]~i', $this->current());
}
}
class AlphaCharFilter extends FilterIterator
{
public function accept()
{
return ctype_alpha($this->current());
}
}
class FileCharacterIterator extends SplFileObject
{
public function current()
{
return $this->fgetc();
}
}
lol wow the simplicity is astounding but is it fast enough to handle multiple requests. as i’am trying to stay away from high leveled classes of php as the are slow compared to native language commands and some helpful functions mixed in as needed.
as well what is faster fread() set to get by 1 or fgetc()?.
i know both are deep system calls so whats the lesser of to evils?
You won’t experience any speed degradation if you were to use the class provided by Anthony and Salathe so that’s no reason to stay away from such methods of coding.
As for the second question, in what context would you be using file accessing?
Given that I know nothing of your needs, and that the question is very vague (more than one request, oh no!), the generic answer is yes. Only you can really answer the question (and even then, perhaps not conclusively).
If you’re interested in any differences to satisfy academic curiosity, then by all means go and do some tests yourself. If your code needs to be so fine-tuned as to be actually considering the difference between those two, perhaps PHP isn’t the right tool and reading through a file (or files) on every execution of the script certainly isn’t. If, as is usually the case, you’re thinking about this topic prematurely then I wouldn’t worry about it at all.