Re: Regular Expression: Markup Code
| From: | Tom Rogers | Date: | Mon, 13 Sep 2004 23:14:33 +0000 |
| Subject: | Re: Regular Expression: Markup Code | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-196581@lists.php.net to get a copy of this message | ||
Hi,
Tuesday, September 14, 2004, 6:04:44 AM, you wrote:
r> Hello, would someone please help me with my regular expressions? They are so
r> complex!
r> Here is what I need to do. I have a string with contents similar to the
r> following:
r> <BLOCK NAME="TOP">
r> (a bunch of markup code)
r> </BLOCK>
r> In other words, the name of the block is not known at runtime. They could be
r> anything really!
r> I need a way to parse through this string, and make new strings with names
r> like $TOP, $BOTTOM, $WAYOVERHERE, that contain their markup code, but not the
r> <BLOCK> tags.
r> Any help would be greatly appreciated!
r> -Samuel
This should get you started:
<?
function callback($a){
print_r($a);
}
$str = <<<END
<BLOCK NAME="TOP">
(a bunch of markup code)
</BLOCK>
<BLOCK NAME="BOTTOM">
(a bunch of markup code)
</BLOCK>
<BLOCK NAME="WAYOVERHERE">
(a bunch of markup code)
</BLOCK>
END;
preg_replace_callback('!<BLOCK
NAME="(.*?)">(.*?)</BLOCK>!is','callback',$str);
Use the call back function to build whatever you need.
--
regards,
Tom