Jump to content

Recommended Posts

  $text = preg_replace( "#\[spoiler\](.+?)\[/spoiler\]#is", "<input type=button value='Spoiler' onclick=\"if (this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display != '') { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = '';this.innerText = ''; this.value = 'Hide'; } else { this.parentNode.parentNode.getElementsByTagName('div')[1].getElementsByTagName('div')[0].style.display = 'none'; this.innerText = ''; this.value = 'Show'; }\" /><div style=display:none><div class=quote1>Spoiler</div><div class=quote2>\\1</div></div>", $text, 6, $spoilercount );

 

This is my minature bbcode spoiler tag I am making.

 

The problem is, if they use this 2 times, it will work, but if they click on any the first one will always show.

 

I need to give each div a ID....

 

so is there a way where I can use $i++ and give a div a id1, id2, id3, on each iteration of replacing?

 

 

Link to comment
https://forums.phpfreaks.com/topic/268420-can-we-use-a-increment-in-preg_replace/
Share on other sites

Or you could just structure the HTML so that you don't need an ID. Like

    
    
        
    

and CSS like

.spoiler > .spoiler-content { display: none; }
.spoiler.revealed > input { display: none; }
.spoiler.revealed > .spoiler-content { display: block; }

 

To answer the question, use preg_replace_callback and provide a function to do the replacing.

thanks req, but i dont think i can do a "Show/hide" button with that technique so lets scratch that one for now

 

 

maybe i should re-word my question my bad.

 

is there anyway to put this preg_replace in a loop so I can use a $i++; ?

 

i just need to do something like this:

 

 $text = preg_replace( "#\[spoiler\](.+?)\[/spoiler\]#is", "<input type='button' value='Spoiler' onclick=\"if(pe('spoiler$i++;').style.display=='none') {pe('spoiler').style.display=''}else{pe('spoiler').style.display='none'}\"><div id='spoiler' style='display:none'>\\1</div>", $text, 6, $spoilercount );
   

 

see the $i++; ? then ofcourse do that for all id's :P

 

 

 

while( preg_match( "#\[spoiler\](.+?)\[/spoiler\]#ies", $text ) )
            {
		$spoiler++;
       $text = preg_replace( "#\[spoiler\](.+?)\[/spoiler\]#is", "$spoiler<input type='button' value='Spoiler' onclick=\"if(pe('spoiler').style.display=='none') {pe('spoiler').style.display=''}else{pe('spoiler').style.display='none'}\"><div id='spoiler' style='display:none'>\\1</div>", $text, 6, $spoilercount );
   
   }

 

the $spoiler++ only produces 1 and 1, not 1,2

 

not working damn :P im getting closE!

Or you could just structure the HTML so that you don't need an ID. Like

<div class="spoiler">
    <input type="button" value="Spoiler" onclick="this.parentNode.className='spoiler revealed';" />
    <div class="spoiler-content">
        <!-- content -->
    </div>
</div>

and CSS like

.spoiler > .spoiler-content { display: none; }
.spoiler.revealed > input { display: none; }
.spoiler.revealed > .spoiler-content { display: block; }

 

To answer the question, use preg_replace_callback and provide a function to do the replacing.

 

DUDE HELL YA

 

I took your code and slammed it like a BAWS

 

function spoiler(a){


a.parentNode.className == 'spoiler' ? a.parentNode.className='spoiler revealed' : a.parentNode.className='spoiler';

}

 

<div class="spoiler">
    <input type="button" value="Spoiler" onclick="spoiler(this)"/>
    <div class="spoiler-content">
      hey
    </div>
</div>

 

CSS:

 

.spoiler > .spoiler-content { display: none; }

.spoiler.revealed > .spoiler-content { display: block; }

 

now no need to worry about id's/etc LOVE IT!

 

TOPIC SOLVED BABY!!! THANK YOU LOL :D

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.