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
https://forums.phpfreaks.com/topic/174087-solved-html-parsing/
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
https://forums.phpfreaks.com/topic/174087-solved-html-parsing/#findComment-917683
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
https://forums.phpfreaks.com/topic/174087-solved-html-parsing/#findComment-917686
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.