티스토리 뷰

$string = '한글<iframe src="http://www.youtu.be/dQw4w9WgXcQ"></iframe>dfjasdflajksl';
$pattern = 'src=["']?(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&"'>]+)["']?';
preg_match("/{$pattern}/i", $string, $matches);
echo ($matches[0]); //only the src="http://www.youtu.be/dQw4w9WgXcQ part
echo ($matches[1]); //the video id part. (dQw4w9WgXcQ)

참고


댓글