Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_match problem 1

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
US
I'm using the regex
Code:
/<!\-\-\s+BEGIN (.+?)\s+\-\->(.+?)!\-\-\s+END %s\s+\-\->/ims
with preg_match_all() to catch all of the HTML code that are found within the comments
Code:
<!-- BEGIN whatever -->random HTML code<!-- END whatever -->

My results are as follow:
Code:
Array
(
    [0] => Array
        (
            [0] => <!-- BEGIN table -->
TABLE
  <!-- BEGIN table_row -->
    ROW
  <!-- END table_row -->
<!-- END table -->
        )

    [1] => Array
        (
            [0] => table
        )

    [2] => Array
        (
            [0] => 
TABLE
  <!-- BEGIN table_row -->
    ROW
  <!-- END table_row -->

        )

)

After parsing this HTML file:
Code:
<html>

<head>
    <title>{TITLE}</title>
</head>

<body>

<!-- BEGIN table -->
TABLE
  <!-- BEGIN table_row -->
    ROW
  <!-- END table_row -->
<!-- END table -->

</body>

</html>

What I want is:
Code:
Array
(
    [0] => Array
        (
            [0] => <!-- BEGIN table -->
TABLE
  <!-- BEGIN table_row -->
    ROW
  <!-- END table_row -->
<!-- END table -->
        )

    [1] => Array
        (
            [0] => <!-- BEGIN table_row -->
    ROW
  <!-- END table_row -->

        )

)

What am I doing wrong with the regex?

---------------------------------------
 
i spent quite a bit of time playing with the regex when you first posted this and came up with no regex solution. of course this doesn't mean there isn't one!

could you try and approach this issue another way? what is the larger goal you are trying to achieve? is it impossible to change the templating code?

btw a lot of templating systems use regex. i also use a forms generating class called html_quickform that uses an element template of:

Code:
 var $_elementTemplate = 
        "\n\t<tr>\n\t\t<td align=\"right\" valign=\"top\"><!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required --><b>{label}</b></td>\n\t\t<td valign=\"top\" align=\"left\"><!-- BEGIN error --><span style=\"color: #ff0000\">{error}</span><br /><!-- END error -->\t{element}</td>\n\t</tr>";

and renders it using the regex
Code:
$html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);

maybe this might help as something approximating what you are trying to achieve, although in this case the tags are not nested.
 
i'm not too experienced in regex.

would this suffice for your needs?
Code:
<?
$html="<!-- BEGIN table -->
TABLE
  <!-- BEGIN table_row -->
    ROW
  <!-- END table_row -->
<!-- END table -->";
$array[] = "table";
$array[] = "table_row";
echo "<pre>";
for ($i=0; $i<count($array);$i++):
$pattern="/([ \t\n\r]*)?<!-- BEGIN ".$array[$i]." -->(\s|\S)*<!-- END ".$array[$i]." -->([ \t\n\r]*)?/i";
preg_match_all($pattern, $html, $matches);

print_r ($matches);
echo "<br/>";
endfor;
echo "</pre>";
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top