views:

106

answers:

6

I have seen some solutions, or at least tries, but none of them really work.

How do I strip all tags except those inside <code> or [code] - and replace all the < and > with &lt; etc. in order to let js do some syntax highlighting on the output?

A: 

The strip_tag sintaxe gives you an ioption to determine the allowable tags: string strip_tags ( string $str [, string $allowable_tags ] ) -> from PHP manual.

This should give you a start on the right direction I hope.

Ronaldo Junior
sure, but what about the tags inside the allowed tags? They are still stripped - but I want them to be converted
Sorry, I miss your point - its not only the <code> tag but all inside it. Some of the other answers might help you more.
Ronaldo Junior
A: 

Why don't you try using strpos() to get the position of [code] and [/code].

When you have the location (assuming you only have one set of the code tag) just get the contents of everything before and everything after and the strip_tags on that text.

Hope this helps.

AntonioCS
That'll work great until I type "hey - look at this code!" That said, there's not really a good solution to this problem (other than getting the HTML-encoding right in the first place, and encoding stuff within `<code></code>` blocks correctly).
Dominic Rodger
so youre good then? hook me up...
David Morrow
A: 
<?php
$str = '<b><code><b><a></a></b></code></b><code>asdsadas</code>';
$str = str_replace('[code]', '<code>', $str);
$str = str_replace('[/code]', '</code>', $str);
preg_match('/<code>(.*?)<\/code>/', $str, $matches);
$str = strip_tags($str, "<code>");

foreach($matches as $match)
{
    $str = preg_replace('/<code><\/code>/', $str, '<code>'.htmlspecialchars($match).'</code>', 1);
}
echo $str;
?>

This searches for the code tags and captures what is within the tags. Strips the tags. Loops through the matches replacing the code tags with the text captured and replacing the < and >.

EDIT: the two str_replace lines added to allow [code] too.

Gazler
A: 

Use a callback:

$code = 'code: <p>[code]<hi>sss</hi>[/code]</p> more code: <p>[code]<b>sadf</b>[/code]</p>';

function codeFormat($matches)
{
    return htmlspecialchars($matches[0]);
}

echo preg_replace_callback('@\[code\](?:(?!\[/code\]).)*\[/code\]@',  'codeFormat', $code);
webbiedave
A: 
    $str = '[code]
        <script type="text/javascript" charset="utf-8">
            var foo = "bar";
        </script>
        [/code]
        <a href="should get removed">strip me</a>';

echo formatForDisplay( $str );

function formatForDisplay( $output ){
    $output = preg_replace_callback( '#\[code]((?:[^[]|\[(?!/?code])|(?R))+)\[/code]#', 'replaceWithValues', $output );
    return strip_tags($output);
}

function replaceWithValues( $matches ){
    return htmlentities( $matches[ 1 ] );
}

try this should work, i tested it and it seemed to have the desired effect.

David Morrow
I get this: var foo = "bar"; strip meit still strips all tags, also the ones inside [code]
Seems to work too, I accidentally striped tags on submit, before passing the entered value
next mission: pass a lang-parameter in the code - ex: [code lang=php]
at the moment I am using your code like this: ´public function formatForDisplay( $output ){ $output = preg_replace_callback( '#\[code lang=(php|js|css|html)]((?:[^[]|\[(?!/?code])|(?R))+)\[/code]#', array($this,'replaceWithValues'), $output ); return strip_tags($output,'<code>'); } public function replaceWithValues( $matches ){ return '<code class="'.$matches[ 1 ].'">'.htmlentities( $matches[ 2 ] ).'</code>'; }´
A: 

Well, I tried a lot with all your given code, right now I am working with this one, but it is still not giving the expected results - What I want is, a regular textarea, where one can put regular text, hit enter, having a new line, not allowing tags here - maybe <strong> or <b>.... Perfect would be to recognice links and have them surrounded with <a> tags

This text should automatically have <p> and <br /> where needed.

To fill in code in various languages one should type [code lang=xxx] code [/code] - in the best case [code lang="xxx"] or <code lang=xxx> would work too. Than typing the code or copy and paste it inside.

The code I am using at the moment, that at least does the changing of tags and output it allright except of tabs and linebreaks is:

public function formatForDisplay( $output ){
    $output = preg_replace_callback( '#\[code lang=(php|js|css|html)]((?:[^[]|\[(?!/?code])|(?R))+)\[/code]#', array($this,'replaceWithValues'), $output );
    return strip_tags($output,'<code>');
}

public function replaceWithValues( $matches ){
    return '<code class="'.$matches[ 1 ].'">'.htmlentities( $matches[ 2 ] ).'</code>';
}

Similar like it works here.