2012-11-08 2 views
1

제 간단한 컨텐트 관리 시스템에 버튼을 추가했습니다. 기사 등을 써서 사진과 비디오를 추가하고 싶습니다. 그리고 사용자 정의 태그로 사용하여 추가 할디스 플레 이미지 또는 비디오의 사용자 정의 태그를 PHP로 사용하기

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 링크를 작성하면 페이지를 표시 할 때 동영상을 표시합니다.

어떻게하면됩니까?

+0

youtube 비디오를 표시하려면 youtube의 사용자 정의 소스 코드를 삽입해야합니다. http://support.google.com/youtube/bin/answer.py?hl=ko&answer=171780을 참조하십시오. –

+0

커스텀 태그와 어떻게 통합합니까? – Seyhan

+0

사용자 정의 태그의 의미를 이해하지 못합니다. –

답변

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; 

희망이 있습니다.

+0

PHP 함수를 사용하여 어떻게하면됩니까? 이 코드에서 작동하지 않습니다, 일치하는 오류가 발생합니다. – Seyhan

+0

어떤 오류 메시지가 표시됩니까? –

+0

이 스크립트의 실용적인 데모를 작성해주십시오. – Seyhan

0

EDIT : OP 코드는 PHP 코드 응답이 필요하다는 것을 명확히했습니다. (다른 답변 참조). 다른 사람들이 JS 솔루션을 원한다면 여기에 남겨주세요.

힘든 시간이 코드를 (jsfiddle 나를 위해 작동하지 않습니다) 테스트를 갖는,하지만 난이 일을해야한다고 생각 :

을 유튜브 비디오의 경우 :

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>'); 
}​ 

당신이 원하는 것 어떤 이미지와 비슷합니다. 요소 내의 요소에 대한 참조가있는 경우 요소가 손상됩니다. 희망이 도움이됩니다.

+0

사이의 모든 이미지 파일에 대해서도 동일한 작업을 수행하려고합니다. 테스트 할 것이므로 에 대한 html 코드가 작동합니다. 이미지를 코드에 포함시킬 수 있다면 – Seyhan

+0

이미지와 함께 작동하도록하려면 이미지 고유의 코드를 비디오 고유 코드로 대체하면됩니다. 비디오 코드가 작동하는지 알려 주시면 이미지에 대한 코드를 포함하도록 내 대답을 편집 할 수 있습니다. –

+0

요소 ID는 무엇입니까? 그리고이 코드를 뷰 내용 페이지에 어떻게 추가 할 수 있습니까? 이게 기능인가요? – Seyhan

관련 문제