1

RSSフィード内のすべてのURLに追加の文字列(「/ tests123」など)を追加しようとしています。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

ただし、次のように置き換えられたときに、最後に追加文字列が含まれる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

?>
4

3 に答える 3

2

preg マッチング/置換の代わりに、 XPathDOMDocumentを使用できます

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

もちろん、XML の例はばかげています。以下のコードは相対リンクをチェックし、それらを絶対リンクにします。

$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();

これにより、変換された HTML が吐き出されます。

<xml>
<items>
<item>
<link href="http://testing/123">
<link href="http://test">
<font><tag>x</tag></font>
</item>
</items>
</xml>
于 2012-10-10T14:31:22.900 に答える
1

$temp の配列を保持する別の変数が必要です。

それで

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

次に、後で $match をエコーし​​ます (for ループまたは for each ループ内)。


または、一致を文字列として保持し、同様に追加します

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

?>
于 2012-10-10T13:39:23.443 に答える
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";
}

?>

基本的には、正規表現で行をフィルタリングし、テキストを追加するインデックスの両側を抽出します。

次に、すべてを連結します。

于 2012-10-10T14:25:38.173 に答える