Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace links in an XML feed 1

Status
Not open for further replies.

Baldwin238

Technical User
Feb 23, 2005
68
US
I am at my wits end. I am using a feed on my site. It contains links as part of the feed. However, I want to replace the links in the feed, with links of my own. Here is the code I use to put the feed onto my site:
Code:
<?php 
$file = '[URL unfurl="true"]http://www.freerolls.net/feed.php';[/URL] 
require_once('xml.parser.class.php'); 

$xml = new xml_parser_class(); 
$xml->parseFile($file); 
$tree = $xml->getTree(); 
unset($xml);

print '<table border="0" cellpadding="2" cellspacing="0" width="" class="tm">';
print '<tr class="caption"><td>Time (Eastern)</td><td>Payouts</td><td>Site</td></tr>';


$i = 0;
foreach($tree['freerolls']['tournament'] as $k)


{
   $i++;
   print '<tr class="'.(!($i%2)?'rl':'rd').'">';
   print '<td>'.$k['time']['value'].'</td>';
   print '<td>'.$k['payout']['value'].'</td>';
   print '<td><a href="'.$k['sitelink']['value'].'">'.$k['sitename']['value'].'</td>';
   print '</tr>';
}
print '</table>';

?>

Each ['sitelink'] (as it appears from the feed) looks like this:
Code:
<sitelink>[URL unfurl="true"]http://www.freerolls.net/fclick/fclick.php?fid=XXX</sitelink>[/URL]

I want to replace every occurence of that link with:
Code:
<sitelink>[URL unfurl="true"]http://www.absolutelyfreerolls.com/fclick/fclick.php?id=XXX</sitelink>[/URL]

I am an asp person myself and cannot come to grips with how to replace that string. The only part of the link I want to replace is UP TO THE = sign. The "ID" part at the end can stay the same. So I am just looking to replace the main chunk of the url.

I was thinking I could use str_replace, but no matter which way I try it, I cannot seem to get it to work. I am throwing out my LIFELINE here.... HELP!!!

Jeff Baldwin
sig2.gif
 
try this
Code:
<?
$html = "<sitelink>[URL unfurl="true"]http://www.freerolls.net/fclick/fclick.php?fid=XXX</sitelink>";[/URL]
$newhtml = str_ireplace("[URL unfurl="true"]www.freerolls.net","www.absolutelyfreerolls.net",[/URL] $html);
echo htmlspecialchars($newhtml);
?>

the htmlspecialchars() is just to output the stuff on the screen in a readable form.
 
Thanks for the reply! Right after I posted, I tried one more time with a clear head and nailed it. Not sure if it was the best way, BUT it works. I didnt try your method, however I thank you anyway! -- Jeff (this is what I did:)

Code:
      <?php 
$file = '[URL unfurl="true"]http://www.freerolls.net/feed.php';[/URL] 
require_once('xml.parser.class.php'); 

$xml = new xml_parser_class(); 
$xml->parseFile($file); 
$tree = $xml->getTree(); 
unset($xml);


print '<table id="main_table" border="1">';
print '<tr><td class="title">Time (Eastern)</td><td class="title">Payouts</td><td class="title">Site</td></tr>';


$i = 0;
foreach($tree['freerolls']['tournament'] as $k)



{
   $i++;
[b]   $k['sitelink'] = str_replace("[URL unfurl="true"]http://www.freerolls.net/fclick/fclick.php?fid=","http://www.absolutelyfreerolls.com/fclick/fclick.php?id=",$k[/URL]['sitelink']);[/b]
   print '<tr class="'.(!($i%2)?'rl':'rd').'">';
   print '<td>'.$k['time']['value'].'</td>';
   print '<td>'.$k['payout']['value'].'</td>';
   print '<td><a href="'.$k['sitelink']['value'].'">'.$k['sitename']['value'].'</td>';
   print '</tr>';
}
print '</table>';

?>

Jeff Baldwin
sig2.gif
 
pretty much the same method (str_replace) although i used the case insensitive function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top