PHP if语句在preg_replace中不起作用

时间:2017-02-23 10:25:07

标签: php if-statement preg-replace line

我有一个简单的PHP声明,但它一直给我'内部500'。 任何人都可以看到这段代码有什么问题吗?

(没有' if')

$fullyfiltered = preg_replace('/<span>(.*?)<\/span>/', '<div class="chat-message ' if('$1'=="MichaelD"){'me'}else{'chat-midnightblue'}'"><div class="chat-contact"><img src="/assets/demo/avatar/tswan.png" alt=""></div><div id="chat-text" class="chat-text">$1: ', $nearlyfiltered);

编辑 - 完整脚本:

<script>
    setInterval(function(){
    document.getElementById('chat-text').innerHTML = '';
    <?php
    $fh = fopen('chat.txt','r');
    while ($line = fgets($fh)) {
    //echo "<p>" . $line . "</p>";
    $filtered = str_replace("'", "\\'", $line);
    $almostfiltered = str_replace("<span></span>\n", "", $filtered);
    $nearlyfiltered = trim(preg_replace('/\s\s+/', ' ', $almostfiltered));
    $fullyfiltered = preg_replace('/<span>(.*?)<\/span>/', '<div class="chat-message ' if('$one'=="MichaelD"){'me'}else{'chat-midnightblue'}'"><div class="chat-contact"><img src="/assets/demo/avatar/tswan.png" alt=""></div><div id="chat-text" class="chat-text">$1: ', $nearlyfiltered);
    if(!empty($fullyfiltered)){
        $endingp = "</div></div>';"; 
    } else {
        $endingp = "';";
    }
    echo "document.getElementById('chat-text').innerHTML = document.getElementById('chat-text').innerHTML + '" . $fullyfiltered . $endingp;
    }
    fclose($fh);
    ?>      
    },5000);
</script>

1 个答案:

答案 0 :(得分:1)

回调必须是一个函数,而不仅仅是随机脚本部分。请阅读手册(http://php.net/manual/en/function.preg-replace-callback.php