duanqiao1949 2012-10-10 13:33
浏览 47
已采纳

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

?>
  • 写回答

3条回答 默认 最新

  • dtewnsdf47253 2012-10-10 14:25
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀