PHP在RSS提要中附加所有URL

问题描述:

I am trying to append all urls in a RSS feed with a extra string (like "/testing123" etc). The original url format from the RSS looks like this:

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

etc, I used regexp with str_replace in a for loop but i cannot seem to get it working correctly and if i use preg_replace i get errors. When i just echo out the urls with the string appended it appears how i want but when i use str_replace then the urls looks like this instead:

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

I need the urls with the append string at the end when they get replaced however like this:

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

The code i have is:

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

?>

我试图将所有网址附加到带有额外字符串的RSS源中(例如“/ testing123”等)。 来自RSS的原始url格式如下所示: p>

 &lt; link rel =“alternate”type =“text / html”href =“http://website.com  / item / name1 / 2162561“/&gt; 
&lt; link rel =”alternate“type =”text / html“href =”http://website.com/item/name2/2162435“/&gt; 
  代码>  pre> 
 
 

等,我在for循环中使用了带有str_replace的regexp,但我似乎无法使其正常工作,如果我使用preg_replace,我会收到错误。 当我只是用附加的字符串回显url时,它显示我想要的但当我使用str_replace时,url看起来像这样: p>

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

我需要带有追加字符串的网址 当它们被替换时结束,如下所示: p>

 &lt; link rel =“alternate”type =“text / html”href =“http://website.com/  item / name1 / 2162561 / testing123“/&gt; 
&lt; link rel =”alternate“type =”text / html“href =”http://website.com/item/name2/2162435/testing123“/&gt; \  n  code>  pre> 
 
 

我的代码是: p>

 &lt;?php 
 
 //追加字符串\  n $ append ='/ testing123'; 
 
 //文件
 $ file =“RSS.txt”; 
 
 //获取文件内容
 $ contents = file_get_contents($ file); \  n 
 //搜索模式
 $ SearchPattern ='/ href=["|\'((。[^] ||'] +)/ i'; 
nn运行preg_match_all以获取所有 Matches 
preg_match_all($ SearchPattern,$ contents,$ Matches); 
 
  //检查我们是否至少有1个匹配
 $ MatchCount = count($ Matches [0]); 
 
 //如果匹配次数超过1,则运行for循环
if($ MatchCount&gt  ;  0){
 for($ i = 0; $ i&lt; $ MatchCount; $ i ++){
 
 $ temp = $ Matches [0] [$ i]; 
 echo $ temp。  $ append。  '&lt; br /&gt;';  //似乎工作
 
 // $ contents = str_replace($ temp,$ temp。$ append,$ contents);  //但是str_replace似乎不起作用
 
 // preg_replace($ temp,$ temp。$ append,$ contents);  //并使用preg_replace给出错误
 
}; 
}; 
 
echo $ contents;  //显示内容
 
?&gt; 
  code>  pre> 
  div>

This should work :

<?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]."
";
}

?>

Basically, it filters the lines with regular expressions and extracts both sides of the index where you want to append text.

It then concatenates it all.

you need another variable to hold an array of $temp.

so

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

then echo $match later (in a for loop or for each loop)

Or keep match as a string and append the
as well

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

?>

Instead of preg matching/replacing, you could use XPath and DOMDocument

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

Example XML is absurd, of course. Code below checks for relative links, and makes them absolute.

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

That spits out transformed HTML:

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