테스트 사이트 - 개발 중인 베타 버전입니다

유튜브 아이디 얻는 정규식에 질문이 있습니다. 채택완료

열혈처리 3년 전 조회 1,492

안녕하세요.

 

프로알라를 쓰고 있습니다... 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개

채택된 답변
+20 포인트
3년 전

</p>

<p><?php</p>

<p>$wrt = [

    'wr_content' => '<p><a href="<a href="https://youtu.be/a_hnClZp4hs"" target="_blank" rel="noopener noreferrer">https://youtu.be/a_hnClZp4hs"</a> id="isPasted"><a href="https://youtu.be/a_hnClZp4hs</a></p><p>
</p><p>본문에" target="_blank" rel="noopener noreferrer">https://youtu.be/a_hnClZp4hs</a></p><p>
</p><p>본문에</a> 글씨 있음</p><p>
</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="<a href="https://www.youtube.com/embed/a_hnClZp4hs?&wmode=opaque&rel=0"" target="_blank" rel="noopener noreferrer">https://www.youtube.com/embed/a_hnClZp4hs?&wmode=opaque&rel=0"</a> frameborder="0" allowfullscreen="" class="fr-draggable"></iframe></span></p><p>본문</p>',

    'b' => '<p><a href="<a href="https://youtu.be/a/b/c/a_hnClZp4hs"" target="_blank" rel="noopener noreferrer">https://youtu.be/a/b/c/a_hnClZp4hs"</a> id="isPasted"><a href="https://youtu.be/a_hnClZp4hs</a></p><p>
</p><p>본문에" target="_blank" rel="noopener noreferrer">https://youtu.be/a_hnClZp4hs</a></p><p>
</p><p>본문에</a> 글씨 있음</p><p>
</p>',

];</p>

<p> </p>

<p>$regexp = '%youtu\.?be(?:\.com)?.*?([^/"?]+)(?=["?])%i';</p>

<p> </p>

<p>preg_match($regexp, $wrt['wr_content'], $match);

print_r($match);</p>

<p> </p>

<p>preg_match($regexp, $wrt['a'], $match);

print_r($match);</p>

<p> </p>

<p>preg_match($regexp, $wrt['b'], $match);

print_r($match);</p>

<p>?></p>

<p>

</p>

<p>/*</p>

<p>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

)</p>

<p>*/</p>

<p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

열혈처리
3년 전
감사합니다.. 잘적용하여 원하는봐 해결 했습니다. 다른분들도 이걸 검색해서 도움 얻었으면 좋겠네요.

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인