Menu
I have a script which processes the output of TinyMCE. I added a custom <code></code> tag in TinyMCE. However, TincyMCE still uses <p> and <div> to create the line breaks. This breaks validation because they cannot be inside a <code> tag. I want to strip_tag() everything in the <code></code>. Here’s what I have so far (it does not work):
[code=php]preg_match_all(‘/(<code>)(.*)(</code>)/’,$content,$results,PREG_PATTERN_ORDER);
foreach($results[1] as $result){
$code=strip_tags($result);
$content = preg_replace(‘/<code>.*</code>/’,'<code>’.$code.'</code>’,$content)
}
[code=php]
preg_match_all('/<code>(.*)</code>/',$content,$results,PREG_PATTERN_ORDER);
$content='<code>';
foreach($results[1] as $result){
$content.=preg_replace('/(<.+?>)/', '',$result);
}
$content .= '</code>';
[/code]
[code=php]
preg_match_all('/(.*?)?<code>(.*?)?</code>(.*?)?/',$content,$results,PREG_SET_ORDER);
$newContent='';
$trailer = FALSE;
foreach($results as $result){
$newContent.=$result[1];
$newContent.='<code>' . preg_replace('/(<.+?>)/', '',$result[2]) . '</code>';
if (isset($result[3])) $trailer = TRUE;
}
if ($trailer) {
preg_match('/.*</code>(.*)/', $content, $tail);
}
$content = $newContent . $tail[1];
[/code]
[CODE]
preg_replace('/<(/?)code>/,"$1FOOBAR",$content);
$content=strip_tags($content);
preg_replace('/(/?)FOOBAR/,"<$1code>",$content);
[/CODE]
[code]
[/code], but the tags inside get converted to plain text. However, in my case, my TinyMCE editor automatically adds <br>'s, <div>'s, and other tags inside the <code></code>, I want to remove them. This way, tags are normal outside the <code>, but there are no tags inside the <code>.[code=php]
function pop_source_between_tags($string,$open,$close,$repl) {
if (substr_count($string,$open) == 0) return $repl;
if ( (substr($string,0,strlen($open)) != $open) && (substr_count($string,$open)) ) {
$e = substr($string,0,strpos($string,$open));
$string = substr($string,strlen($e));
return pop_source_between_tags($string,$open,$close,$repl); }
$part = substr($string,strpos($string,$open), strpos($string,$close)+strlen($close));
$repl[] = $part;
$new = substr($string,(strlen($close)+strpos($string,$close)));
if (substr_count($string,$close) >= 1) { return pop_source_between_tags($new,$open,$close,$repl); }
else { return $repl; }
}
function yo_dawg_i_heard_you_didnt_like_tags($block,$tag) {
$endtag = "</" . substr($tag, 1);
$z = pop_source_between_tags($block,$tag,$endtag,array());
foreach($z as $y => $x) {
$block = str_replace($z[$y],$tag.strip_tags($z[$y]).$endtag,$block);
}
return $block;
}
[/code]
<i>
</i>$content='<p>Here is some text </p> and some more text<code>and a little code snipped<p> with embedded tags</p> and another tag<h4>over here</h4> and this will be the end of the code section</code> but not the end of the whole message.<p>In fact, we can add another code section here if we like, like this: <code><h2>This is another code section...</h2></code>, and finally, some more plain text<p>with tags</p>.';
This forum doesn't replace tags... it just replaces < and > with < and > (among some other things im sure, they're html entities)[/quote]
[code=php], , etc) because that has sort of become the standard for embedding a specific subset of tags in a forum.
This would probably be.... 5 lines in python :/[/QUOTE]
[code=php]htmlentities($string);
? See [code=php]//To reduce risk of errors
if(preg_match('/<code>/',$content)){
preg_match_all('/(.*?)?<code>(.*?)?</code>(.*?)?/',$content,$results,PREG_SET_ORDER);
$newContent='';
$trailer = FALSE;
foreach($results as $result){
$newContent.=$result[1];
$newContent.='<code>' . preg_replace('/(<.+?>)/', '',$result[2]) . '</code>';
if (isset($result[3])) $trailer = TRUE;
}
if ($trailer) {
preg_match('/.*</code>(.*)/', $content, $tail);
}
$content = $newContent . $tail[1];
}[/code]
[code=php]
$content='Here is a little content upon which to test this code';
$tempContent = $content;
$content = '';
$j = strlen($tempContent);
$i = 0;
while ($i < $j) {
$ch = $tempContent[$i];
if (ord($ch) > 128) $ch = ' ';
$content .= $ch;
$i++;
}
[/code]
[code ][/code ]. Do you know of any way to strip all tags within a certain tag in TinyMCE? I created the "codebox" button in TinyMCE by duplicating the "blockquote" button and changing <blockquote></blockquote> to <code></code>. However, tags are still allowed in blockquote.
My form does not use BBCodes. TinyMCE generates all of the HTML tags.
[code=php]
$tempContent = $content;
$content = '';
$j = strlen($tempContent);
$i = 0;
while ($i < $j) {
$ch = $tempContent[$i];
if ((ord($ch) < 32) || (ord($ch) > 127)) $ch = ' ';
$content .= $ch;
$i++;
}
preg_match_all('/(.*?)?<code>(.*?)?</code>(.*?)?/',$content,$results,PREG_SET_ORDER);
$newContent='';
$trailer = FALSE;
foreach($results as $result){
$newContent.=$result[1];
$newContent.='<code>' . preg_replace('/(<.+?>)/', '',$result[2]) . '</code>';
if (isset($result[3])) $trailer = TRUE;
}
if ($trailer) {
preg_match('/.*</code>(.*)/', $content, $tail);
}
$content = $newContent . $tail[1];
[/code]
[code=php]if(preg_match('/<code>/',$content)){
preg_match_all('/(.*?)?<code>(.*?)?</code>(.*?)?/',$content,$results,PREG_SET_ORDER);
$newContent='';
$trailer = FALSE;
foreach($results as $result){
$newContent.=$result[1];
$newContent.='<code>' . strip_tags($result[2]) . '</code>';
if (isset($result[3])) $trailer = TRUE;
}
if ($trailer) {
preg_match('/.*</code>(.*)/', $content, $tail);
}
$content = $newContent . $tail[1];
}[/code]
[code=php]
if(preg_match('/<code>/',$content)){
$tempContent = $content;
$content = '';
$j = strlen($tempContent);
$i = 0;
while ($i < $j) {
$ch = $tempContent[$i];
if ((ord($ch) < 32) || (ord($ch) > 127)) $ch = ' ';
$content .= $ch;
$i++;
}
preg_match_all('/(.*?)?<code>(.*?)?</code>(.*?)?/',$content,$results,PREG_SET_ORDER);
$newContent='';
$trailer = FALSE;
foreach($results as $result){
$newContent.=$result[1];
$newContent.='<code>' . preg_replace('/(<.+?>)/', '',$result[2]) . '</code>';
if (isset($result[3])) $trailer = TRUE;
}
if ($trailer) {
preg_match('/.*</code>(.*)/', $content, $tail);
}
$content = $newContent . $tail[1];
}
[/code]
[code=php]// Code tags in the main content
if(preg_match('/(<code>)/',$content,$match)){
$i = $a = count($match)-1;
while($i>0) {
$content = preg_replace('/<code>(.*?)</code>/','<code'.$i.'>$1</code'.$i.'>',$content,1);
$i--;
}
while($a>0){
preg_match('/<code'.$a.'>(.*?)</code'.$a.'>/',$result);
$content = preg_replace('/<code'.$a.'>(.*?)</code'.$a.'>/','<code>'.preg_replace('/(<.+?>)/', '',$result[1]).'</code>');
$a--;
}
}[/code]
[code=php]// Code tags in the main content
if(preg_match('/<code>/',$content)){
$i = $a = preg_match_all('/<code>/',$content,$match);
while($i>0) {
$content = preg_replace('/<code>/','<code'.$i.'>',$content,1);
$content = preg_replace('/</code>/','</code'.$i.'>',$content,1);
$i--;
}
while($a>0){
//debug
$content = $content.$a;
preg_match('/<code'.$a.'>(.*?)</code'.$a.'>/',$content,$result);
echo $result[1];die;
$content = preg_replace('/<code'.$a.'>(.*?)</code'.$a.'>/','<code>'.preg_replace('/(<.+?>)/', '',$result[1]).'</code>',$content);
$a--;
}
}[/code]
[code=php]if(preg_match('/<code>/',$content)){
$i = $a = preg_match_all('/<code>/',$content,$match);
while($i>0) {
$content = preg_replace('/<code>/','<code'.$i.'>',$content,1);
$content = preg_replace('/</code>/','</code'.$i.'>',$content,1);
$i--;
}
while($a>0){
//debug
$content = $content.$a;
//this line works
preg_match('/(<code'.$a.'>)(.*)(</code'.$a.'>)/',$content,$result);
//error probably lies in this line
$content = preg_replace('/(<code'.$a.'>)(.*?)(</code'.$a.'>)/','<code>'.strip_tags($result[2]).'</code>',$content);
$a--;
}
}[/code]
<i>
</i>$tempContent = $content;
$content = '';
$j = strlen($tempContent);
$i = 0;
while ($i < $j) {
$ch = $tempContent[$i];
if (ord($ch) > 128) $ch = ' ';
$content .= $ch;
$i++;
}
[code=html]tes<strong>ting</strong>
<code>
<div>te<strong>stin</strong>g</div>
</code>
<div><strong>tes</strong>ting</div>
<code>
<div>te<strong>stin</strong>g</div>
</code>[/code]
[code=html] tes<strong>ting</strong>
<br><code2>
<br><div>te<strong>stin</strong>g</div>
<br></code2>
<br><div><strong>tes</strong>ting</div>
<br><code1>[/code]
[code=html]tes<strong>ting</strong><code><div>te<strong>stin</strong>g</div></code><div><strong>tes</strong>ting</div><code><div>te<strong>stin</strong>g</div></code>[/code]
[code=html]tes<strong>ting</strong><code>testing</code><div><strong>tes</strong>ting</div><code>testing</code>[/code]
0.1.9 — BETA 5.18