Jump to content
#FlattenTheCurve ×
Semnomic

file_get_contents How to target the url only with replace function ?

Recommended Posts

$rss = file_get_contents('www.somesite.com/rss');
$rssnew = str_replace('AAAA', 'BBB', $rss );

Hi got a sImple bit of code to replace text in the $rss,

 

how do i target the url only? as it is now its replacing all instances of "AAAA" in urls,text,links,image urls,etc.

 

 

Share this post


Link to post
Share on other sites

Whatever do you mean by the 'url only'???  Do you mean the actual url address?

Share this post


Link to post
Share on other sites

Hi - the url for the link in the feed, say it was a news rss - all items in feed have a url (link) to that article

 

It could be called GUID not sure so dont want to confuse things.

 

TEST LINK

 

I need a way to make str_replace only replace parts of the url (not image url OR title etc)

Share this post


Link to post
Share on other sites

Since your second post wasn't any clearer than the first, I"ll try and understand.  You're saying that the contents of $res are not simply being "replace" but the links inside of $res are themselves being parsed and also being "replaced"?  That doesn't make sense to me.  Have you tried echoing the contents of $res to see that it is what you think it should be?

Share this post


Link to post
Share on other sites

OK lets try again.
 
I have a page on my site >>  rssrewriter.php
 
I visit this url >>  mysite.com/rssrewriter.php?somerssfeed.rss
 
I then use the code I posted to rewrite the rss and echo it out.
 
When it rewrites the rss it changes ALL instances of my "str_replace" throughout the whole outputted rss.
 
I want to limit the rewriting to only the url/ link - not the image,desc,etc. see below
 
Element Description Example
title The title of the item. Venice Film Festival Tries to Quit Sinking
link The URL of the item. http://nytimes.com/2004/12/07FEST.html
description The item synopsis. Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged.
author Email address of the author of the item. More. oprah\@oxygen.net
category Includes the item in one or more categories. More.
comments URL of a page for comments relating to the item. More. http://www.myblog.org/cgi-local/mt/mt-comments.cgi?entry_id=290
enclosure Describes a media object that is attached to the item. More.
guid A string that uniquely identifies the item. More. http://inessential.com/2002/09/01.php#a2
pubDate Indicates when the item was published. More. Sun, 19 May 2002 15:21:36 GMT
source The RSS channel that the item came from. More.

 

 

So i only want rewrite parts of the items in red not the whole thing.

Edited by Semnomic

Share this post


Link to post
Share on other sites

 

So i only want rewrite parts of the items in red not the whole thing.

 

str_replace will replace everything found in the supplied string, which in your case is the whole rss file.

 

Split it into parts so that you can change those parts that you want to, simplexml will help you to do that.

  • Like 1

Share this post


Link to post
Share on other sites

str_replace will replace everything found in the supplied string, which in your case is the whole rss file.

 

Split it into parts so that you can change those parts that you want to, simplexml will help you to do that.

thanks i did see the last link you posted (same link) but was hoping there was some other way to do it like explode or something along those lines. I will try and work out simple xml as it is basically exploding it/doing exactly what i want. 

Edited by Semnomic

Share this post


Link to post
Share on other sites

If you want to split a file using explode or str_replace, then you need to examine it first to see how the contents are laid out so that you can work out how to split it up.

 

Why do you want to change a few lines rather than just re-generate the file?

Share this post


Link to post
Share on other sites

Its a remote/dynamic file, it changes all the time and Im not in control of the original.

 

Im feeding a rss to a second site as if it was my own, spoofing the orig RSS.  But I need to rewrite the original urls so they fit my sites structure.

 

 

"-"s where very problematic and i needed to remove them but doing so removed them from image urls and at times also the html/text/article body messing up the new rss I was outputting.

 

I think im good now

Edited by Semnomic

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.