Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用自定义标记显示图像或视频_Php_Javascript_Html_Forms_Tags - Fatal编程技术网

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

在php中使用自定义标记显示图像或视频,php,javascript,html,forms,tags,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.sel

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

    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]');" /> 
函数formatText(el,tagstart,tagend){
如果(标高设置选择范围){
el.value=el.value.substring(0,el.selectionStart)+标记开始+el.value.substring(el.selectionStart,el.selectionEnd)+标记结束+el.value.substring(el.selectionEnd,el.value.length)
}
否则{
var selectedText=document.selection.createRange().text;
如果(selectedText!=“”){
var newText=tagstart+selectedText+tagend;
document.selection.createRange().text=newText;
} 
}
} 

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


我该怎么做呢?

编辑:OP最终澄清了需要一个PHP代码答案(参见我的另一个答案)。如果其他人需要JS解决方案,请将此项保留在此处。

测试这段代码很困难(JSFIDLE不适合我),但我认为这应该是可行的:

对于youtube视频:

var el = document.getElementById('<your element id>');
while(true){
    var match = '/\[VIDEO\](https?:\/\/)?(www.)?youtube.com(\/watch\?v=|\/?\?)?(\w*)\[\/VIDEO\]/im.exec(el.innerHTML);
    if(match==null)break;
    el.innerHTML=el.innerHTML.replace(match[0],'<iframe width="420" height="315" src="http://www.youtube.com/embed/'+match[1]+'" frameborder="0" allowfullscreen></iframe>');
}​
var el=document.getElementById(“”);
while(true){
var match='/\[VIDEO\](https?:\/\/)?(www.)?youtube.com(\/watch\?v=\/?\)?(\w*)\[\/VIDEO\]/im.exec(el.innerHTML);
如果(匹配==null)中断;
el.innerHTML=el.innerHTML.replace(匹配[0],“”);
}​

您可能希望对图像执行类似的操作。请注意,如果对元素中的任何元素有任何引用,它们将被打断。希望这有帮助。

这段代码应该可以工作。更多解释请参见注释

//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;
//此变量的数据将从数据库、用户输入等中读取。
$content=“非奥古斯都,ultrices[VIDEO]www.youtube.com/?vvdee[/VIDEO]ut elementum natoque temporal placerat.Egestas,penatibus urna,dis risus habitase.Rhoncus augue urna porta,enim cum risus eu tortor in,p[IMAGE]images/abcde.png[/IMAGE]我们的母亲是一位母亲,她是一位母亲,母亲是一位母亲,母亲是一位母亲,母亲是一位母亲,母亲是一位母亲,母亲是一位母亲,母亲是一位母亲,母亲是一位母亲,母亲是一位母亲;
//将自定义视频标记替换为youtube嵌入
preg\u match\u all('/\[VIDEO\](https?:\/\/)?(www.)?youtube.com(\/watch\?v=\/?)?(\w*)\[\/VIDEO\]/im',$content,$matches,preg\u SET\u ORDER);
foreach($matches作为$match进行匹配){
$content=str_replace($match[0],“”,$content);
}
//用html图像标记替换自定义图像标记
预匹配所有('/\[IMAGE\](.*)\[\/IMAGE\]/im',$content,$matches,预设置顺序);
foreach($matches作为$match进行匹配){
$content=str_replace($match[0],“”,$content);
}
echo$内容;

希望这有帮助。

要显示youtube视频,您必须插入youtube的自定义嵌入代码。请参阅。如何与自定义标记集成?我不明白自定义标记的含义。如何将[VIDEO][/VIDEO]自动识别为视频文件?请再次阅读我的问题,是否要在html中搜索[VIDEO][/VIDEO]并嵌入所指的youtube视频?我将对此进行测试,因此html代码可能适用于图像,如果我可以将其包含在codesRight中,则要使其适用于图像,您只需将特定于图像的代码替换为特定于视频的代码。如果您让我知道视频代码是否有效,我可以尝试编辑我的答案以包含图像代码。元素id是什么?如何将此代码添加到查看内容页?这是一个函数吗?您的自定义标记将位于哪个元素中?全身任何地方?如果是,则更改
document.getElementById(“”)
document.body
。您是想在服务器上还是在客户端上运行代码?如何使用php函数来执行此操作?这在我的代码中不起作用,匹配给出了错误。@Seyhan终于开始测试我的代码并发现/修复了一系列错误。请参阅我更新的答案以获取固定代码和演示。谢谢jdwire,看起来很完美。另外,我想了解如何定义preg_match_all以从中删除http://watch?v=link@Seyhan尝试将正则表达式字符串更改为
/\[VIDEO\](https?:\/\/)?(www.)?youtube.com(\/watch\?v=\/?\?)?(\w*)\[\/VIDEO\]/im
,然后查看是否有效。