1

이 코드입니다 :정규 표현식 (으로, preg_match_all) 및 배열

<?php 
    $matchWith = "# http://videosite.com/id1 http://videosite.com/id2 # # http://videosite.com/id3 http://videosite.com/id4 #"; 

    preg_match_all('%(#)\shttp://videosite\.com/(\w+)\r{0,1}\shttp://videosite\.com/(\w+)\r{0,1}\s(#)%', $matchWith, $result); 
    foreach($result[2] as $value) 
    { 
    print '<a href="http://videosite.com/'.$value.'"> 
    First Part 
    </a>';   

    } 
    foreach($result[3] as $value) 
    { 
    print '<a href="http://videosite.com/'.$value.'"> 
    Second Part 
    </a>';   
    } 
    ?> 

문제점 :

나는 그것을 다음과 같이 표시 할 : 첫 번째 부분, 두 번째 부분과 첫 번째 부분은 두 번째 부분.

그러나 첫 번째 부분, 첫 번째 부분, 두 번째 부분, 두 번째 부분으로 표시됩니다.

나는 정말로 당신이 이해하기를 바랍니다. 감사합니다.

답변

2
<?php 
    $matchWith = "# http://videosite.com/id1 http://videosite.com/id2 # # http://videosite.com/id3 http://videosite.com/id4 #"; 

    preg_match_all('%(#)\shttp://videosite\.com/(\w+)\r{0,1}\shttp://videosite\.com/(\w+)\r{0,1}\s(#)%', $matchWith, $result); 
    for($i = 0; $i < count($result[2]); $i++){ 
     print '<a href="http://videosite.com/'.$result[2][$i].'"> 
     First Part 
     </a>'; 
     print '<a href="http://videosite.com/'.$result[3][$i].'"> 
     Second Part 
     </a>';  
    } 
    ?> 

두 개의 배열이 병렬 구조로되어 있다고 가정합니다. 편집

: $ 변경 나는이 당신이 원하는 무엇인가 내가

+0

for 루프는'$ i =='대신'$ i ++'라고 써야하지만 그 외 올바른 것입니다. –

+0

굉장! 내가 찾고 있던 바로 그 대답! 너 락! :) – Helena

0

를 ++ $에 ==?

for($i = 0; $i < count($result); $i++) 
{ 
    print '<a href="http://videosite.com/'.$result[$1][2].'"> 
    First Part 
    </a>';   

    print '<a href="http://videosite.com/'.$result[$1][3].'"> 
    Second Part 
    </a>'; 
} 
+0

오류 메시지가 표시되기 때문에 잘 모르겠습니다. 그러나 누군가 내가 방금 찾던 답을 게시했습니다. 아직도 고마워. :) – Helena