2012-10-10 3 views
1

추가 문자열 (예 : "/ testing123"등)이 포함 된 RSS 피드의 모든 URL을 추가하려고합니다. 은 RSS에서 원래 URL 형식은 다음과 같습니다PHP RSS 피드에 모든 URL 추가

<link rel="alternate" type="text/html" href="http://website.com/item/name1/2162561"/> 
<link rel="alternate" type="text/html" href="http://website.com/item/name2/2162435"/> 

는 등, 내가 for 루프에서 않는 str_replace와 정규 표현식을 사용하지만 난 그것을 제대로 작동하고 내가 preg_replace이다를 사용하는 경우 내가 오류를 얻을 얻을 수없는 것. 난 그냥 문자열의 URL을 반향하는 경우 추가 내가 방법을 표시하지만 난 않는 str_replace를 사용할 때 다음의 URL 대신 다음과 같습니다

http://website.com/testing123/item/name1/2162561 
http://website.com/testing123/item/name2/2162435 

나는 그들이 얻을 끝에 APPEND 문자열로 URL을 필요 이 같은 그러나 대체 : 내가 가지고

<link rel="alternate" type="text/html" href="http://website.com/item/name1/2162561/testing123"/> 
<link rel="alternate" type="text/html" href="http://website.com/item/name2/2162435/testing123"/> 

코드는 다음과 같습니다

<?php 

// The append string 
$append = '/testing123'; 

// The file 
$file = "RSS.txt"; 

// Get the files contents 
$contents = file_get_contents($file); 

// The search pattern 
$SearchPattern = '/href=["|\'](.[^"|\']+)/i'; 

// Run preg_match_all to grab all the Matches 
preg_match_all($SearchPattern, $contents, $Matches); 

// Check to see if we have at least 1 match 
$MatchCount = count($Matches[0]); 

// If there is more than 1 match then run a for loop 
if ($MatchCount > 0) { 
    for ($i=0; $i < $MatchCount ; $i++) { 

      $temp = $Matches[0][$i]; 
      echo $temp . $append . '<br />'; // Appears to work 

      //$contents = str_replace($temp, $temp . $append, $contents); // But str_replace doesn't seem to work 

      //preg_replace($temp, $temp . $append, $contents); // And using preg_replace gives a error 

    }; 
}; 

echo $contents; // Display the contents 

?> 
+0

원래 조각은 잘 작동하는 것 같다. – Ifthikhan

+0

아니요, website.com 다음에 추가 문자열을 배치하고 있지만 URL 끝에 가야합니다. – zeddex

답변

1


를 문자열로 일치를 유지하고 추가이 작동합니다 :

<?php 

// The append string 
$append = '/testing123'; 

// The file 
$file = "RSS.txt"; 

// Get the files contents 
$contents = file_get_contents($file); 

// The search pattern 
$SearchPattern = '/(<link .* href=".*)("\/>)/i'; 

// Run preg_match_all to grab all the Matches 
preg_match_all($SearchPattern, $contents, $matches); 

for($i=0;$i<count($matches[1]);$i++){ 
    echo $matches[1][$i].$append.$matches[2][$i]."\n"; 
} 

?> 

기본적으로 정규 표현식으로 줄을 필터링하고 텍스트를 추가 할 색인의 양면을 추출합니다.

그런 다음 모두 연결합니다. 대신 레그 매칭

+0

감사합니다. 원본 URL을 대체하기 위해 수정해야하는 부분이 있습니다. 내가 마지막으로 변경 한 것은 ($ i = 0; $ i zeddex

1

당신은 $ 온도의 배열을 유지하기 위해 다른 변수가 필요합니다.

so

$ match [i] = $ temp. $ append;

다음 에코 $의 (각 루프 루프 나에 대한)에 이후 경기

또는뿐만 아니라

// If there is more than 1 match then run a for loop 

if ($MatchCount > 0) { 
    for ($i=0; $i < $MatchCount ; $i++) { 

      $temp = $Matches[0][$i]; 
      $match .= $temp . $append . '<br />'; // Appears to work 

      //$contents = str_replace($temp, $temp . $append, $contents); // But str_replace doesn't seem to work 

      //preg_replace($temp, $temp . $append, $contents); // And using preg_replace gives a error 

    }; 
}; 

echo $match; // Display the contents 

?> 
+0

미안합니다. $ temp = $ Matches [0] [$ i]; \t \t $ match [i] = $ temp. $ append; \t \t echo $ match; 루프에서 오류가 발생했습니다 – zeddex

+0

원하는 부분 만 저장하지 마십시오. 당신 대신에 총을 대체하고 에코. 해달라고 말하면, 그냥 범위를 벗어난 변수에 할당하십시오. – exussum

+0

고맙지 만 코드를 사용할 때 href = "와 함께 1 개의 URL 만 출력합니다. 그러나 전에는 많은 URL이 있어야합니다. – zeddex

2

/교체, 당신은 XPath를DOMDocument를

$html = <<< EOF 
<xml> 
    <items> 
    <item> 
     <link href="/testing/123" /> 
     <link href="http://test" /> 
     <font><tag>x</tag></font> 
    </item> 
    </items> 
</xml> 
EOF; 

예 XML은 물론, 터무니을 사용할 수 있습니다. 아래의 코드는 상대 링크를 확인하고 절대적으로 만듭니다. 변환 HTML을 내뿜

$doc = new DOMDocument(); 
@$doc->loadXML($html); 
$xpath = new DOMXpath($doc); 

$links = $xpath->query("//link"); 
for($i = 0; $i < $links->length; $i++) { 
    $href = $links->item($i)->getAttribute('href'); 
    if(substr($href, 0, 4) != 'http') { 
     $links->item($i)->setAttribute('href', "http://" . ltrim($href, '/')); 
    } 
} 

echo $doc->saveHTML(); 

:

<xml> 
<items> 
<item> 
<link href="http://testing/123"> 
<link href="http://test"> 
<font><tag>x</tag></font> 
</item> 
</items> 
</xml> 
+0

방금 ​​Florian F의 코드에 필요한 답변을 찾았지만 게시물에 감사드립니다. – zeddex

관련 문제