Sign in to follow this  
doctorhair1

[web] PHP preg_match all expressions?

Recommended Posts

Hey everyone, I have searched php.net for preg_match and found a wealth of information. However I am missing on two pieces of information that I was hoping some guru on here could help me with. One is I want to know how to use preg_match to detect the content between a set of code like this: <trlSIAD::start> <td ...> ... </td> </tr> ... </trlSIAD::start> I want to get all the data between <trlSIAD::start> and </trlSIAD::start> I know you can do something simular with preg_match("/<trlSIAD::start>(.*?)<\/trlSIAD::start>/"...); but then it all has to be on one line. I want to be able to structure it exactly as it appears above. Second, does anyone know where to find all the examples of preg_match/replace? I just see billions of different combinations and I don't know what any of them mean except the basic ones like [a-zA-Z0-9] and (.*). Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by doctorhair1
I know you can do something simular with preg_match("/<trlSIAD::start>(.*?)<\/trlSIAD::start>/"...); but then it all has to be on one line.


Nope. The preg_* family of functions can handle multiline strings just fine. For good examples read the user comments on php.net on the various preg_ pages.

Share this post


Link to post
Share on other sites
Well I did, I found some great examples on finding single line stuff, but none that handeled multiline stuff. Do you have a link maybe? I searched the preg_match page and teh preg_replace...no luck. Thanks though for the help so far. If you could give me a link to read on about that multiline problem I would appreciate it.

Share this post


Link to post
Share on other sites
nvm, I got it, thanks I searched a little more on google.com by putting in PHP multiline preg_match and finally came up with something. I was forgetting the /s switch. Thanks again :)

Share this post


Link to post
Share on other sites
Regex isn't my strong side, but I think this will work. This is not tested.

^(\<trlSIAD\:\:start\>(.*?)\<\/trlSIAD\:\:start\>)'is

<edit> You posted before... </edit>

Share this post


Link to post
Share on other sites
I think you'll quickly find that writing a proper parser will be less of a headache than a series of regular expressions. It also makes for a nice programming exercise.

You could just search for a < character with strpos, and then kick into parser mode. It would then look like:

// namespace::tag

if ($this->accept(Token::SLASH))
{
$closing = true;
}

$this->expect(Token::WORD);
$namespace = $tag = $this->symbol->content;

if ($this->accept(Token::COLON))
{
$this->expect(Token::WORD);
$tag = $this->symbol->content;
}
else
$namespace = NULL;

$this->accept(Token::WHITE_SPACE);

if (!$closing)
{
// search for attributes
$attributes = array();

while ($this->accept(Token::WORD))
{
$attr_name = $attr_value = $this->symbol->content;
$this->accept(Token::WHITE_SPACE);

if ($this->accept(Token::EQUAL))
{
$this->accept(Token::WHITE_SPACE);
$this->expect(Token::STRING);
$attr_value = $this->symbol->content;
}

$attributes[$attr_name] = $attr_value;

$this->accept(Token::WHITE_SPACE);
}

if ($this->accept(Token::SLASH)) $self_close = TRUE;
}
$this->expect(Token::GT);

if ($namespace == 'trlSIAD')
{
switch ($tag)
{
case "start":
break;
}
}




Basically, "expect" means the next symbol must be next, else an error is triggered. If it is what you expect, then it will be returned and the next symbol is grabbed. "accept" just means that if the next symbol is what you specify, then it will return it and advance to the next.

I just wrote it on the spot, so there could be mistakes, but the overall method should be clear. All you need to do is provide a Parser class that can identify and return the next token.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this