开发者

Strip tags but not those inside <code>

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 JavaScri开发者_如何学Pythonpt do some syntax highlighting on the output?


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.


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);


<?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.


    $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.


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.


The strip_tag syntax gives you an option 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜