Semnomic Posted September 16, 2017 Share Posted September 16, 2017 $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. Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/ Share on other sites More sharing options...
ginerjm Posted September 17, 2017 Share Posted September 17, 2017 Whatever do you mean by the 'url only'??? Do you mean the actual url address? Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551413 Share on other sites More sharing options...
Semnomic Posted September 17, 2017 Author Share Posted September 17, 2017 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) Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551417 Share on other sites More sharing options...
ginerjm Posted September 17, 2017 Share Posted September 17, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551431 Share on other sites More sharing options...
archive Posted September 17, 2017 Share Posted September 17, 2017 You can create a simple xml object and then go through that? But as said, you have not explained what you want to do or why you would want to do it. Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551451 Share on other sites More sharing options...
Semnomic Posted September 17, 2017 Author Share Posted September 17, 2017 (edited) 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 Exampletitle The title of the item. Venice Film Festival Tries to Quit Sinkinglink The URL of the item. http://nytimes.com/2004/12/07FEST.htmldescription 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.netcategory 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=290enclosure 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#a2pubDate Indicates when the item was published. More. Sun, 19 May 2002 15:21:36 GMTsource 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 September 17, 2017 by Semnomic Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551458 Share on other sites More sharing options...
archive Posted September 17, 2017 Share Posted September 17, 2017 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551460 Share on other sites More sharing options...
Semnomic Posted September 17, 2017 Author Share Posted September 17, 2017 (edited) 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 September 17, 2017 by Semnomic Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551462 Share on other sites More sharing options...
archive Posted September 18, 2017 Share Posted September 18, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551502 Share on other sites More sharing options...
Semnomic Posted September 22, 2017 Author Share Posted September 22, 2017 (edited) 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 September 22, 2017 by Semnomic Quote Link to comment https://forums.phpfreaks.com/topic/305005-file_get_contents-how-to-target-the-url-only-with-replace-function/#findComment-1551778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.