Jump to content
• Advertisement

[web] PHP preg_match all expressions?

This topic is 4420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Share on other sites
Advertisement
Quote:
 Original post by doctorhair1I know you can do something simular with preg_match("/(.*?)<\/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

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

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

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

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

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5
• Advertisement

• 10
• 10
• 9
• 9
• 10
• Forum Statistics

• Total Topics
631752
• Total Posts
3002089
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!