note 31067 added to function.ereg
| From: | ron dot petty at unigeek dot com | Date: | Tue, 08 Apr 2003 22:09:50 +0000 |
| Subject: | note 31067 added to function.ereg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46377@lists.php.net to get a copy of this message | ||
I notice there was no iterative example listed here. If you need to search for patterns on a string
more than once (which I have), you need to be sure to calculate the correct string to search.
Example.
$s = "abababa";
while(ereg("b",$s,$temp))
{
$t = $temp[0];
$start = strlen($t) + strpos($s,$t);
$length = strlen($s);
$s = substr($s,$start,$length-$start);
print $t."\n";
}
This will print out three b's. If you do not want to change the variable that you are
searching just make a local copy before the loop. It would be nice if there were functions that
would tell you where a pattern was found instead of using strpos for a found pattern. Oh well it
all works out in the end. And it does work for big complicated regular expressions and search
strings, I am parsing cvs version files (rcs format) and their regular expressions look like (from
my code)
$this->w = "(\t|\n|\v|\r| )*"; // \ b and \ f are messing it up look like
literals?
$this->num = "([0-9]|\.)+";
$this->id = '(([0-9]|\.)+)*[^?,.:;@]([^?,.:;@]|([0-9]|\.)+)*';
$this->sym = '([0-9])*[^$,.:;@]([^$,.:;@]|([0-9]))*';
$this->idchar = '[^$,.:;@]';
$this->special = '$,.:;@';
$this->string = '@([^@]|[@]{2})*@';
$this->delta_num = "$this->w$this->num$this->w";
$this->delta_date = "date$this->w$this->num;$this->w";
$this->delta_author = "author$this->w$this->id;$this->w";
$this->delta_state = "state$this->w($this->id)?;$this->w";
$this->delta_branches =
"branches$this->w($this->num|$this->w)*;$this->w";
$this->delta_next = "next$this->w($this->num)?;$this->w";
$this->delta =
"($this->delta_num$this->delta_date$this->delta_author$this->
delta_state$this->delta_branches$this->delta_next)*";
$this->single_delta = "($this->delta_num$this->delta_date$this->delta_author
$this->delta_state$this->delta_branches$this->delta_next)";
In my example you give an RCS file (cvs version file like file,v) and this function is called
function getDelta()
{
$temp = array();
$two_array = array();
$f = $this->filecontent;
while(ereg($this->single_delta,$f,$temp))
{
$two = $temp[0];
$start = strlen($two) + strpos($f,$two);
$length = strlen($f);
$f = substr($f,$start,$length-$start);
$two = trim($two);
$two_array[] = $two;
}
}
filecontent is the file content from file,v, and $this->single_delta is from above. So you can
imagine it works pretty nicely even with complex stuff.
--
http://www.php.net/manual/en/function.ereg.php
http://master.php.net/manage/user-notes.php?action=edit+31067
http://master.php.net/manage/user-notes.php?action=delete+31067
http://master.php.net/manage/user-notes.php?action=reject+31067