Şerit etiketleri ama bu iç

6 Cevap php

Ben bazı çözümler gördüğüm, ya da en azından dener, ama bunların hiçbiri gerçekten çalışmak var.

Nasıl o iç dışındaki tüm etiketleri şerit yok <code> veya [code] - ve tüm < ve > olarak değiştirin {[(4) }] vb JavaScript çıkışında vurgulayarak bazı sözdizimi yapalım için?

6 Cevap

Neden [code] konumunu ve [/ code] almak için strpos() kullanmayı deneyin.

Eğer konumu (sadece kod etiketinin bir set var varsayarak) olduğunda hemen önce her şeyin içeriğini almak ve sonra her şey ve bu metin üzerinde strip_tags.

Umarım bu yardımcı olur.

Bir geri arama kullanın:

$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;
?>

Bu kod etiketleri arar ve etiketleri içinde ne olduğunu yakalar. Etiketlerini kaldırır. Yakalanan metin ile kod etiketleri değiştirilmesi ve yerine maç üzerinden döngüler < ve>.

EDIT: İki str_replace hatları [code] de izin eklendi.

    $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 ] );
}

Bu çalışması gerekir deneyin, ben de denedim ve istenen etkiye sahip gibi görünüyordu.

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

Gerektiğinde bu metin otomatik var <p> ve <br /> olmalıdır.

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.

Ben en azından tamam sekmeler ve linebreaks hariç etiketleri ve çıkış bunun değişen yaptığı, şu anda kullanıyorum kodu:

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>';
}

Bunun gibi benzer burada çalışıyor.

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

Bu size umarım doğru yönde bir başlangıç ​​vermelidir.