Jump to content

[SOLVED] HTML tags on multiple lines


thegman

Recommended Posts

Hi All,

I need to remove all divs which have 'style="DISPLAY: none" '.

 

	<DIV id=radiobtn31 g="1">Hosting Required
        <DIV id=radiobtn3100 g="2">    Plan A - 3.99/month
            <DIV id=radiobtn321 g="3">         1 Year</DIV>
            <DIV id=radiobtn322 style="DISPLAY: none" g="3">         2 Years</DIV>
    	</DIV>
        
        <DIV id=radiobtn3101 style="DISPLAY: none" g="2">    Plan B - 8.99/month
            <DIV id=radiobtn323 style="DISPLAY: none" g="3">         1 Year</DIV>
            <DIV id=radiobtn324 style="DISPLAY: none" g="3">         2 Years</DIV>
        </DIV>
      
        <DIV id=radiobtn3102 style="DISPLAY: none" g="2">    Plan C - 15.99/month
            <DIV id=radiobtn325 style="DISPLAY: none" g="3">         1 Year</DIV>
            <DIV id=radiobtn326 style="DISPLAY: none" g="3">         2 Years</DIV>
        </DIV>
   	</DIV>

 

Since they are nested, I tried deleting them inside out ie. Divs with g=3 first then g=2 and so on. The problem I encounter is after ejecting the div with g=3 i get the div with g=2 on different lines

 

eg.

        <DIV id=radiobtn3102 style="DISPLAY: none" g="2">    Plan C - 15.99/month
            
        </DIV>

 

Now, when i try to do the same for g=2 i get an error since the start and end tags are on different lines.

 

The code I am using is as follows...but it just doesnt seem to work

$string = preg_replace("<DIV.*DISPLAY.*none.*g=\"3\".*/DIV>", "", $string);

$string = preg_replace("<DIV.*DISPLAY.*none.*g=\"2\".*/DIV>", "", $string);

echo $string;

 

I am a n00b at this and would appreciate any and all help in this regard.

 

Many Thanks and best regards,

G

 

Link to comment
Share on other sites

You need the /s modifier to include new lines in ..

 

<pre>
<?php
$string = <<<DATA
<DIV id=radiobtn31 g="1">Hosting Required
        <DIV id=radiobtn3100 g="2">    Plan A - 3.99/month
            <DIV id=radiobtn321 g="3">         1 Year</DIV>
            <DIV id=radiobtn322 style="DISPLAY: none" g="3">         2 Years</DIV>
    	</DIV>
        
        <DIV id=radiobtn3101 style="DISPLAY: none" g="2">    Plan B - 8.99/month
            <DIV id=radiobtn323 style="DISPLAY: none" g="3">         1 Year</DIV>
            <DIV id=radiobtn324 style="DISPLAY: none" g="3">         2 Years</DIV>
        </DIV>
      
        <DIV id=radiobtn3102 style="DISPLAY: none" g="2">    Plan C - 15.99/month
            <DIV id=radiobtn325 style="DISPLAY: none" g="3">         1 Year</DIV>
            <DIV id=radiobtn326 style="DISPLAY: none" g="3">         2 Years</DIV>
        </DIV>
   	</DIV>
DATA;

$string = preg_replace('%<DIV[^>]+DISPLAY[^>]+none[^>]+g="3"[^>]*>.*?</DIV>%s', '', $string);
$string = preg_replace('%<DIV[^>]+DISPLAY[^>]+none[^>]+g="2"[^>]*>.*?</DIV>%s', '', $string);
echo htmlspecialchars($string);

?>
</pre>

Link to comment
Share on other sites

Dear effigy,

I had been struggling with the code for almost 3 days and it was driving me insane. Your solution works perfectly

 

I cant put into words how much I appreciate your help with this....but ill try...

 

THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!THANK YOU!...you rock man!

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.