php - 在 php 中使用自定义标签显示图像或视频

标签 php javascript html forms tags

我在表单中添加了按钮,就像我的简单内容管理系统一样。我写文章等,所以我也想添加图片和视频。我想使用自定义标签添加它们

    function formatText (el,tagstart,tagend) { 

    if (el.setSelectionRange) {
    el.value = el.value.substring(0,el.selectionStart) + tagstart + el.value.substring(el.selectionStart,el.selectionEnd) + tagend + el.value.substring(el.selectionEnd,el.value.length)
    }
    else {
    var selectedText = document.selection.createRange().text;

    if (selectedText != "") { 
        var newText = tagstart + selectedText + tagend; 
        document.selection.createRange().text = newText; 
    } 

    }



} 


<form name="my_form"> 
<textarea name="my_textarea" id="mytext"></textarea><br /> 
<input type="button" value="IMAGE" onclick="formatText (document.getElementById('mytext'),'[IMAGE]','[/IMAGE]');" /> 

这是将 [IMAGE] [/IMAGE] 标签添加到我的文本区域中,我想在提交表单后显示此标签之间的图像链接。同样,我想添加 [VIDEO] [/VIDEO] 标签,如果我在这些标签之间写入 YouTube 链接,则在显示页面时显示视频。

我怎样才能做到这一点?

最佳答案

这段代码应该可以工作。请参阅评论以获取更多说明。

//The data for this variable would be read from a database, user input, etc.
$content = "Non augue, ultrices[VIDEO]www.youtube.com/?vvddee[/VIDEO] ut elementum natoque tempor placerat. Egestas, penatibus urna, dis risus habitasse. Rhoncus augue urna porta, enim cum risus eu tortor in, p[IMAGE]images/abcde.png[/IMAGE]lacerat rhoncus mus tortor? Nec, magnis, enim elementum non urna eros augue, ridiculus porta dapibus? Parturient, dictumst nunc vel turpis! Eu eros vel tempor cum in.";

//Replace custom video tag with youtube embed
preg_match_all('/\[VIDEO\](https?:\/\/)?(www.)?youtube.com(\/watch\?v=|\/?\?)?(\w*)\[\/VIDEO\]/im', $content, $matches, PREG_SET_ORDER);
foreach($matches as $match){
    $content = str_replace($match[0],'<iframe width="420" height="315" src="http://www.youtube.com/embed/'.$match[4].'" frameborder="0" allowfullscreen></iframe>',$content);
}

//Replace custom image tag with html image tag
preg_match_all('/\[IMAGE\](.*)\[\/IMAGE\]/im', $content, $matches, PREG_SET_ORDER);
foreach($matches as $match){
    $content = str_replace($match[0],'<img src="'.$match[1].'"></image>',$content);
}
echo $content;

希望这有帮助。

关于php - 在 php 中使用自定义标签显示图像或视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13289486/

相关文章:

html - 主页内容在调整大小时相互移动?

php - 如何根据用户输入从Excel工作表中查找数据

php - 动态PHP/mysql下拉菜单问题

javascript - HTML5如何在Canvas中调用图像

HTML/CSS - Firefox 和 Chrome 以及 IE 之间的问题

html - CSS/HTML - 列宽格式

php - 页面中 URI 中的 XSS 没有任何输入

php - 使用 Gmail 的 SMTP 服务器每天发送 1000 多封电子邮件

javascript - jQuery 中的 Click() 没有执行任何操作

javascript - CSS 或 JS 解决方案 - 在导航菜单中将鼠标悬停在子 LI 上时加粗父 UL