0

我刚刚为我的用户开发了一个 PHP 聊天系统,当他们编写一些带有链接或视频 URL 的文本以显示类似于 facebook 聊天的内容时,我需要。

这是我现在拥有的,但没有工作,我不知道为什么。非常感谢一些帮助。谢谢!

PHP代码:

$str = $row['message'];
 $str = preg_replace_callback('#(?:https?://\S+)|(?:www.\S+)|(?:\S+\.\S+)#', function($arr)
{
    if(strpos($arr[0], 'http') !== 0)
    {
        $arr[0] = 'http://' . $arr[0];
    }
    $url = parse_url($arr[0]);

    // images
    if(preg_match('#\.(png|jpg|gif)$#', $url['path']))
    {
        return '<img src="'. $arr[0] . '" />';
    }
    // youtube
    if(in_array($url['host'], array('www.youtube.com', 'youtube.com'))
      && $url['path'] == '/watch'
      && isset($url['query']))
    {
        parse_str($url['query'], $query);
        return sprintf('<iframe class="embedded-video" src="http://www.youtube.com/embed/%s" allowfullscreen></iframe>', $query['v']);
    }
    //links
    return sprintf('<a href="%1$s">%1$s</a>', $arr[0]);
}, $str);
if($row["sender_userid"] == $from_user_id) {

                $conversation .= '<div class="sent chat">';
                $conversation .= '<div class="chat-body">';
                $conversation .= '<div class="chat-content">';
                $conversation .= '<p>'. $str. '</p>';
                $conversation .= '</div>';
                $conversation .= '</div>';
                $conversation .= '</div>';
            } else {
                $conversation .= '<div class="replies chat chat-left">';
                $conversation .= '<div class="chat-avatar">';
                $conversation .= '<a class="avatar m-0" data-toggle="tooltip" href="#" data-placement="left" title="" data-original-title="">';
                $conversation .= '<img src="'.$toUserAvatar.'" alt="avatar" height="40" width="40"/>';
                $conversation .= '</a>';
                $conversation .= '</div>';
                $conversation .= '<div class="chat-body">';
                $conversation .= '<div class="chat-content">';
                $conversation .= '<p>'. $str. '</p>';
                $conversation .= '</div>';
                $conversation .= '</div>';
                $conversation .= '</div>';
            }

我的固定代码:

$patterns = array();
 $replacements = array();
 $patterns[] = '/((((http|https|ftp|ftps)\:\/\/)|www\.)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(\/\S*)?)/';
 $replacements[] = '<a href="\$1\" target="_blank">$1</a>';
 $patterns[] = '/http(s)?:\/\/youtu\.be\/([^\40\t\r\n\<]+)/i';
 $replacements[] = '<iframe width="420" height="315" src="https://www.youtube.com/embed/$2" frameborder="1" scrolling="no" allowfullscreen></iframe>';
 $patterns[] = '/http(s)?:\/\/(w{3}\.)?youtube\.com\/watch\/?\?v=([^\40\t\r\n\<]+)/i';
 $replacements[] = '<iframe width="420" height="315" src="https://www.youtube.com/embed/$3" frameborder="1" scrolling="no" allowfullscreen></iframe>';
 $patterns[] = '/(?<!src=")(http(s)?:\\/\\/.+(png|jpeg|jpg|gif|bmp))/Ui';
 $replacements[] = '<a data-lightbox="example-1" href="$1" target="_blank"><img height="300" width="440" src="$1"/></a>';

 $text = preg_replace($patterns, $replacements,htmlspecialchars($row['message']));
4

0 回答 0