유튜브 아이디 얻는 정규식에 질문이 있습니다.
본문
안녕하세요.
프로알라를 쓰고 있습니다... https://regex101.com/ 에서는 아이디가 둘다 잘나오는데.. PHP 코드안에서는
결과가 상이합니다..
정규식을 잘몰라서 그런데... 혹시 아시는분 도움즘 구해보겠습니다.
if(!$wtype['as_list']) {
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $wrt['wr_content'], $match);
if($match) {
$wtype['as_list'] = 1;
$wtype['as_thumb'] = "https://img.youtube.com/vi/{$match[1]}/0.jpg";
}
}
------------------------------------------- 정상
<p><a href="https://youtu.be/a_hnClZp4hs" id="isPasted">https://youtu.be/a_hnClZp4hs</a></p><p><br></p><p>본문에 글씨 있음</p><p><br></p>
Array
(
[0] => youtu.be/a_hnClZp4hs
[1] => a_hnClZp4hs
)
--------------------------------------------
-------------------------------------------비정상
<p><span class="fr-video fr-deletable fr-fvc fr-dvb fr-draggable" contenteditable="false" draggable="true"><iframe width="640" height="360" src="https://www.youtube.com/embed/a_hnClZp4hs?&wmode=opaque&rel=0" frameborder="0" allowfullscreen="" class="fr-draggable"></iframe></span></p><p>본문</p>
Array
(
[0] => youtube.com/embed/a_hnClZp4hs?&wmode=opaque&rel=0" frameborder="0" allowfullscreen="" class="fr-draggable"></iframe></span></p><p>유튜
[1] => p><p>유튜
)
답변 1
<?php
$wrt = [
'wr_content' => '<p><a href="https://youtu.be/a_hnClZp4hs" id="isPasted">https://youtu.be/a_hnClZp4hs</a></p><p><br></p><p>본문에 글씨 있음</p><p><br></p>',
'a' => '<p><span class="fr-video fr-deletable fr-fvc fr-dvb fr-draggable" contenteditable="false" draggable="true"><iframe width="640" height="360" src="https://www.youtube.com/embed/a_hnClZp4hs?&wmode=opaque&rel=0" frameborder="0" allowfullscreen="" class="fr-draggable"></iframe></span></p><p>본문</p>',
'b' => '<p><a href="https://youtu.be/a/b/c/a_hnClZp4hs" id="isPasted">https://youtu.be/a_hnClZp4hs</a></p><p><br></p><p>본문에 글씨 있음</p><p><br></p>',
];
$regexp = '%youtu\.?be(?:\.com)?.*?([^/"?]+)(?=["?])%i';
preg_match($regexp, $wrt['wr_content'], $match);
print_r($match);
preg_match($regexp, $wrt['a'], $match);
print_r($match);
preg_match($regexp, $wrt['b'], $match);
print_r($match);
?>
/*
Array
(
[0] => youtu.be/a_hnClZp4hs
[1] => a_hnClZp4hs
)
Array
(
[0] => youtube.com/embed/a_hnClZp4hs
[1] => a_hnClZp4hs
)
Array
(
[0] => youtu.be/a/b/c/a_hnClZp4hs
[1] => a_hnClZp4hs
)
*/