Jump to content

[SOLVED] HTML Parsing


HarryG80

Recommended Posts

Hello,

 

Im trying to build a template system all of its working except my <if> statement variables

 

<?php

$c = '
<if conditional="[LOGGED_IN]">
<a href="logout.php">Logout</a>
</if>
';

preg_match_all('~<if conditional="([^"]+)">([^"]+)</if>~',$c,$matches);

print_r($matches);
?>

 

This will not work because i have the character " in the if statement and for some reason (.*) will not work got any tips on what regex i should use to parse this

 

Thanks

Link to comment
Share on other sites

The dot modifier doesn't match newlines. So you need to use this

 

preg_match_all('~<if conditional="([^"]+)">(.+)</if>~s',$c,$matches);

All I did was put the s modifier on the end, which forces the dot to match all characters, even new lines

Link to comment
Share on other sites

Thanks that works great but now i have another issue if there is more then 1 <if> statement they dont split up into different arrays code example:

 

<?php

$c = '
<if conditional="[LOGGED_IN]">
<a href="logout.php">Logout</a> \' ?? >.../.
</if>

random data <br />

<if conditional="[RATE]">
You have already rated this person
</if>

';

preg_match_all('~<if conditional="([^"]+)">(.+)</if>~s',$c,$matches);

print_r($matches);
?>

 

Output:

Array
(
    [0] => Array
        (
            [0] => <if conditional="[LOGGED_IN]">
<a href="logout.php">Logout</a> ' ?? >.../.
</if>

random data <br />

<if conditional="[RATE]">
You have already rated this person
</if>
        )

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

    [2] => Array
        (
            [0] => 
<a href="logout.php">Logout</a> ' ?? >.../.

</if>

random data <br />

<if conditional="[RATE]">
You have already rated this person

        )

)

 

Thanks Again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.