micah1701 Posted January 31, 2008 Share Posted January 31, 2008 I have a string of text (created by reading a flat file) which looks something like: <?php $myDocument = "<H3>Some Random Header Information</H3> and here is the rest of the document. Blah blah blah you get the idea it goes on and on."; ?> I want to break this into a couple of variables; namely the Header information (which is always contained between an <H3> tag) and the body. So the result would look like: <?php $header = "Some Random Header Information"; $body = "and here is the rest of the document. Blah blah blah you get the idea it goes on and on."; ?> I'm not to great with regular expressions but I'm assuming thats my best bet? My other thought was to "explode" it several times into arrays...but that doesn't seem like it would be in best practice. Thoughts on what I should do? Thanks!!! Quote Link to comment Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 if(preg_match("@<H3>(.*)</h3>(.*)@i",$document,$matches)) { $header=$match[1]; $body=$match[2]; } else if(!isset($matches[1]) || !isset($matches[2])) { echo "Unknown document"; die(); } Quote Link to comment Share on other sites More sharing options...
micah1701 Posted January 31, 2008 Author Share Posted January 31, 2008 Thanks! that's perfect. I've just spent an hour messing around with preg_match and couldn't get it to work. It seems like every regex tutorial has different answers (at least when I try to interpret them). Have a great day!!! Quote Link to comment Share on other sites More sharing options...
laffin Posted January 31, 2008 Share Posted January 31, 2008 I encountered much of the same. Confusion. than i found a nice tool Expresso - Regex development tool its a great tool if yer starting out with regex. the thing to note, when using preg and expresso preg uses start and end delimeters followed by options /pattern/options or @pattern@options while Expresso just uses patterns (checkboxes for the options) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.