HTML을 게시하는 대신 사용자가 입력 한 내용을 YouTube 비디오 링크로 검색 한 다음 코드에서 직접 연결할 수도 있습니다.
나는 오늘 아침에 StackOverflow에이 코드를 발견
How do I find all YouTube video ids in a string using a regex?
그것은 유튜브 URL의 문자열을 검색하고 링크로 대체합니다. 다음은 수정 된 <iframe/>
의
// Linkify youtube URLs which are not already links.
// From https://stackoverflow.com/questions/5830387/php-regex-find-all-youtube-video-ids-in-string
function linkifyYouTubeURLs($text) {
$text = preg_replace('~
# Match non-linked youtube URL in the wild. (Rev:20111012)
https?:// # Required scheme. Either http or https.
(?:[0-9A-Z-]+\.)? # Optional subdomain.
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com followed by
\S* # Allow anything up to VIDEO_ID,
[^\w\-\s] # but char before ID is non-ID char.
) # End host alternatives.
([\w\-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^\w\-]|$) # Assert next char is non-ID or EOS.
[?=&+%\w-]* # Consume any URL (query) remainder.
~ix',
'
<iframe width="560" height="315" src="http://www.youtube.com/embed/$1"></iframe>
',
$text);
return $text;
}
에 URL을 대체하는 코드의 버전 당신은 이런 식으로 구현할 수 있습니다 :
<?php
$text = 'This is my comment. It contains an XSS attack!:
<script type="text/javascript">
alert(\'bam\');
</script>
I learned about XSS on YouTube:
http://www.youtube.com/watch?v=i38LMZyKIqI
';
// Sanitize XSS (e.g.: convert '<' to '<')
$output = htmlspecialchars($text);
$pattern = [];
$output = linkifyYouTubeURLs($output);
// Add natural line breaks
$output = nl2br($output);
echo $output;
?>
XSS 공격 중지,하지만 유튜브 링크 동영상으로 변환됩니다. Vimeo 및 다른 주요 비디오 공급자와 함께 사용하려면이 기능을 더 수정할 수도 있습니다.
http://codepad.viper-7.com/8w0h1F
예, 나는이 작업을 수행 할 수 있습니다 여기에
행동의 코드입니다. 하지만 난 모든 XSS를 방지하려면 :) – warmspringwinds