我正在尝试使用额外的字符串(例如“/ testing123”等)将所有网址附加到RSS Feed中。来自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的regexp,但我似乎无法使其正常工作,如果我使用preg_replace,我会收到错误。当我只是用附加的字符串回显你的网址时,它会显示我想要的但是当我使用str_replace时,网址看起来就像这样:
http://website.com/testing123/item/name1/2162561
http://website.com/testing123/item/name2/2162435
当他们被替换时,我需要在末尾添加追加字符串的网址,但是这样:
<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 :(得分:2)
您可以使用 XPath 和 DOMDocument
而不是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>
答案 1 :(得分:1)
你需要另一个变量来保存$ temp数组。
所以
$ match [i] = $ temp。 $附加;
然后回显$匹配(在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
?>
答案 2 :(得分: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";
}
?>
基本上,它使用正则表达式过滤行,并提取要附加文本的索引的两侧。
然后将它连接起来。