当我在回复或发送消息时尝试调用嵌入式YouTube视频时,我的PHP脚本出现了一些问题。
我的 php脚本是:
<?php
if($_GET['u'] != $user) {
$messages = addslashes($_POST['message']);
$messages = 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']))
{
echo '<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 width="560" height="315" src="http://www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>', $query['v']);
}
//links
return nl2br(sprintf('<a href="%1$s">%1$s </a> ', $arr[0]));
}, $messages);
$to = $_GET['u'];
$selPictureMsg = "SELECT * FROM users WHERE username = '$user'";
$result = mysqli_query($sql, $selPictureMsg);
$msgPicRow = mysqli_fetch_assoc($result);
$selPictureMsg11 = "SELECT * FROM users WHERE username = '$to'";
$result11 = mysqli_query($sql, $selPictureMsg11);
$msgPicRow11 = mysqli_fetch_assoc($result11);
$user_pic = $msgPicRow['profile'];
$receiver_pic = $msgPicRow11['profile'];
if(isset($_POST['msgSend'])) {
if(strlen($messages) <= 1) {
echo "<div class='error'>Sorry! Your message is too small. It needs to be more than 1 character.</div>";
} else {
$insMsg = "INSERT INTO messages VALUES('','$user','$to','$messages','0','$user_pic','$receiver_pic', NOW())";
$sql->query($insMsg);
echo "<div class='success'>Your message has been sent to ".$to." successfully.</div><br />";
}
}
$selmsg1 = "SELECT * FROM messages WHERE (to_user='$user' AND from_user='$to') OR (to_user ='$to' AND from_user='$user') ORDER BY date DESC";
$resultmsg1 = $sql->query($selmsg1);
$rownum = mysqli_num_rows($resultmsg1);
if($rownum > 0) {
$i = '';
echo "<table>
<tr class='chat'>";
while($assocMsg = mysqli_fetch_assoc($resultmsg1)) {
if($user == $_GET['from'] || $user != $assocMsg['to_user']) {
$smilies = array(
':|' => 'bored',
':-|' => 'bored',
':-o' => 'scared',
':-O' => 'scared',
':o' => 'scared',
':O' => 'scared',
';)' => 'wink',
';-)' => 'wink',
':p' => 'tongue',
':-p' => 'tongue',
':P' => 'tongue',
':-P' => 'tongue',
':D' => 'happy',
':-D' => 'happy',
';P' => 'wink-tongue',
';-P' => 'wink-tongue',
':)' => 'smile',
':-)' => 'smile',
':(' => 'sad',
':-(' => 'sad',
'<3' => 'heart',
':*' => 'kiss',
'*_*' => 'in-love',
';(' => 'crying',
':a' => 'angry',
':@' => 'rage',
':3' => 'normal-kiss',
'><' => 'shy',
';D' => 'happily',
';-D' => 'happily',
':$' => 'blushing',
'o_o"' => 'dont-get',
'XD' => 'laugh',
'*lookdown*' => 'look-down',
'*lookup*' => 'look-up',
'*lookleft*' => 'look-left',
'*lookright*' => 'look-right',
'$_$' => 'money',
'$.$' => 'money',
'@_@' => 'nerd',
'@.@' => 'nerd',
'*vomit*' => 'vomit'
);
$replace = array();
foreach ($smilies as $smiley => $imgName)
{
array_push($replace, '<img src="images/emoticons/'.$imgName.'.png" alt="'.$smiley.'" />');
}
$assocMsg["message"] = str_replace(array_keys($smilies), $replace, $assocMsg["message"]);
echo "
<div style='width:100%; max-height: 300px; overflow: auto;'>
<td><a href='profile.php?u=".$assocMsg['from_user']."'><img src='".$assocMsg['user_picture']."' width='auto' height='70px' /></a></td>
<td>".$assocMsg['message']."<br />".$video."</td>
</tr>
<tr>
<td colspan='5'><i>".$assocMsg['date']."</i></td>
</div>
";
$i++;
if($i % 1 == 0) {
echo "</tr><tr class='chat'>";
}
} else {
echo "
<div style='width:100%; max-height: 300px; overflow: auto;'>
<td><a href='profile.php?u=".$assocMsg['to_user']."<img src='".$assocMsg['friend_picture']."' width='auto' height='70px' /></a></td>
<td>".$messages."</td>
</tr>
<tr>
<td colspan='5'><i>".$assocMsg['date']."</i></td>
</div>
";
$i++;
if($i % 1 == 0) {
echo "</tr><tr class='chat'>";
}
}
}
echo "</tr>
</table><hr />";
} else {
echo "You don't have any conversations with $to<br /><hr />";
}
?>
现在,我将在图片中向您展示嵌入式YouTube视频之前和之后的结果。
在: Before embedded youtube video
在: After embedded youtube video
我不知道为什么要剪切存储在数据库中的其他消息,只显示视频,而不显示其他消息。
在这里return sprintf('<iframe width="560" height="315" src="http://www.youtube.com/embed/%s" frameborder="0" allowfullscreen></iframe>', $query['v']);
,我尝试使用echo sprintf(....)
,但echo
没有存储在数据库中。
如果我让它返回它会显示After:
显示的图像。
所以,我想要的是嵌入式YouTube视频,只要您发送视频文字+显示来自聊天的旧信息就会显示。