1

我正在尝试使用额外的字符串(如“/testing123”等)在 RSS 提要中附加所有 url。来自 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 我会出错。当我只是回显带有附加字符串的网址时,它会显示我想要的方式,但是当我使用 str_replace 时,网址看起来像这样:

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

您可以使用XPathDOMDocument代替 preg 匹配/替换

$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 。$追加;

然后回显 $match 稍后(在 for 循环或每个循环中)

或将匹配保留为字符串并
附加

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