Monkuar Posted September 16, 2012 Share Posted September 16, 2012 $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? Quote Link to comment https://forums.phpfreaks.com/topic/268420-can-we-use-a-increment-in-preg_replace/ Share on other sites More sharing options...
requinix Posted September 16, 2012 Share Posted September 16, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268420-can-we-use-a-increment-in-preg_replace/#findComment-1378274 Share on other sites More sharing options...
Monkuar Posted September 16, 2012 Author Share Posted September 16, 2012 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 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 im getting closE! Quote Link to comment https://forums.phpfreaks.com/topic/268420-can-we-use-a-increment-in-preg_replace/#findComment-1378277 Share on other sites More sharing options...
Monkuar Posted September 16, 2012 Author Share Posted September 16, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268420-can-we-use-a-increment-in-preg_replace/#findComment-1378289 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.