Remove part of a URL

Hi,

I have a script that imports RSS feed data into it. The problem is what one of the feed sources has a url with an undefined variable in it (e.g. feed.aspx?id=4&name= ) . The face that the url ends with an equals sign is causing problems with my script. If I remove the “name=” from the url everything works fine.

So my question is how do I remove it?

I don’t know php but I was looked at a similar problem and the solution was to create a php file and link to the feed via the new php file. Eg. new_php_file.php?host=http://domain.com/feed.aspx

The code of the php file was as follows:

[php]<?php
// Configuration
$imagelocation = ‘http://www.domain.com’;
// End Configuration
$host = $_GET[‘host’];
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

echo preg_replace('/<img>(.*?)<\/img>/', '<img>'.$imagelocation.'$1</img>', $response);

?>[/php]

What this code did was to add “http://www.domain.com” to the url in the img tag.

How would I create a new code like this to remove &name= from a url in between

I would really appreciate any help anyone can give me.

Thank you

Well, strpos will find the position of the “?”. This is the argument’s start.
Then, strip off the rest from there, like this…

$url=“xyz.com/?blah
$newurl=substr($url, 0, strpos($url,"?")-1);

Should locate “?”, then subtract 1 from it and get the beginning of the string up to there…
(I didn’t test this, but, try it…)

Hi ErnieAlex,

Thank you for you quick reply.

Well, this is the format of the link: http://domain.com/feed.aspx?id=000&id2=1111&id3=22222&name=
and I only need the “&name=” removed.

With your suggestion wouldn’t the newurl be: http://domain.com/feed.aspx ?

I actually want it to be: http://domain.com/feed.aspx?id=000&id2=1111&id3=22222

Any suggestions?

Thank you

Okay, I did not understand your needs… Well, for that, you have to work it a bit different.
First, there can be one or server arguments. A very simple way would be to just explode it
and implode it without the last arg… Something like this…

$url=“http://domain.com/feed.aspx?id=000&id2=1111&id3=22222&name=blah”;
$parts=explode("&", $url); // break down parts using “&” for deliminator into array
$parts[count($parts)-1]=""; // blank out last array entry (last argument…)
$newurl=implode($parts); // combine them all back without last one…

Tested this one and it works as you want it to… Good luck with your project…

Brilliant :smiley: . That worked! Thanks!

Just one more question. How would I define the $url as the text between and

Also wouldn’t it just be simpler to use preg_replace and delete &name=

I tried reading up on the preg_replace tag but it is quite confusing

would something like this do it:

[php]echo preg_replace(’&name=’, ‘’, $response);[/php]

Well, that would remove the &name=, but, what if something is after it? Then, that part would be stuck in the variable and would mess things up. I do not know the rest of the code and do not know where you input is coming from. I did my version in the blind… PS: Do fix it if it aint broken… LOL It fast enough!

Okay the other issue… I’m not sure what you mean by … Links are for CSS style sheet files. It links them to an external CSS file. Is this what you are talk’n about? It goes something like this:

echo(’’);
// *** Note the quotes and double quotes ***

If you mean you want to put it into a webpage to show a link that someone can click on and go to that address, you would do it with a HREF… Similar for CSS tags. So here is the format for that…

echo(“Here you put the link text (perhaps pull it from the $newurl?)”);

Hope that helps…

Ah yes, I didn’t think of that. Most of the links just have &name= but like you said if there was a value at the end it would get joined with the previous field and cause problems.

Sorry for the confusion the link tag I was referring to was not a html tag but an RSS feed tag.

The feed I have from a provider looks something like this:

[code]

Title Text Blah blah blah http://domain.com/feed.aspx?id=000&id2=1111&id3=22222&name= Title Text 2 Blah blah blah 2 http://domain.com/feed.aspx?id=111&id2=5&id3=777&name= [/code]

What I am really trying to do is to re-print it like this:

[code]

Title Text Blah blah blah http://domain.com/feed.aspx?id=000&id2=1111&id3=22222 Title Text 2 Blah blah blah 2 http://domain.com/feed.aspx?id=111&id2=5&id3=777 [/code]

Thats all.

Well, okay, that makes sense… You already have the url figured out…

To print it from the PHP, just echo it…

echo($newurl); Of course, this depends on your code where you got the rest of it from…

Try to figure it out, if not ask away… That is why we are there… Good luck…

Hi again,

I’ve been cracking my head over this for the past hour and a half with no luck. So I am back here looking for some more help. See the feed I am trying to edit I have no control over. Its just delivered to me in an xml file.

So for example: http://abc.com/feed.xml looks like this:

[code]

Title Text Blah blah blah http://domain.com/feed.aspx?id=000&id2=1111&id3=22222&name= Title Text 2 Blah blah blah 2 http://domain.com/feed.aspx?id=111&id2=5&id3=777&name= [/code]

Now what I am trying to do is create a php file on my site, for example: http://mydomain.com/editfeed.php

I want to enter this file into the browser link: http://mydomain.com/editfeed.php?host=http://abc.com/feed.xml

and I want it to display:

[code]

Title Text Blah blah blah http://domain.com/feed.aspx?id=000&id2=1111&id3=22222 Title Text 2 Blah blah blah 2 http://domain.com/feed.aspx?id=111&id2=5&id3=777 [/code]

I having trouble understanding how to use your code for the url and make those change to the url, while keeping the rest of the feed the same.

Any suggestions?

Thank you

Hmmmm, well, XML is a very structured format. You would either have to “cheat” and replace the links yourself. To do that, you would have to parse thru the code finding all 's and 's. Then, pull the link from each and replace it with your newer version. Then, write the file back out. Not too hard, but tedious. Something, well, searching thru the string using strpos to locate the and then the ending and then locating the ?name= and replacing it with spaces until you reach the “<”. And, then move on to the next one. Not to tricky…

Or, you could design an XML parsing routine to do the change for you and then write out the new file.
Here is a link to the info for doing that project:
http://www.php4every1.com/tutorials/edit-xml/
I think this might be the better way to go because XML is everywhere and it would not hurt to learn how to access it and alter it. The code for this is all in place in this link. OF COURSE, it does not talk about your needs. You will have to parse thru the XML, find every LINK , and change it per our previous discussions. Then, write each one back. Hope that makes sense… Give it a try, off to bed soon.
Post back and let me know what you decide and what you come up with. I am curious how you do…

One more thing on the way to bed… I found this link that has a nice tutorial for you about parsing the XML with DOM which is built into PHP. It has a very nice simple short routine for looping thru an XML file and parsing data. Looks like it would be simple to change to parse your file and alter the links along the way.
Think it might be the way for you to head… Good nite…

http://www.w3schools.com/php/php_xml_dom.asp

Hi ErnieAlex,

You are far to kind. I can’t thank you enough for helping me out and finding me all this information.

I shall go through it and try and solve the problem. I’ll keep you posted on how it turns out.

Thank you once again.

Good night!

Hi Again,

I seem to have hit another stumbling point.

As I had mentioned in the below example editfeed.php is the file I am making and http://abc.com/feed.xml is a feed from an external source.

Well it turns out that the external feed that I need looks like this:

http://abc.com/rss.aspx?r=45

So when list the feed as http://mydomain.com/editfeed.php?host=http://abc.com/rss.aspx?r=45, I think having two question marks in the url is causing the r=45 to be ignored.

Is there a way I can rewrite this url so brings up the r=45 record

for example something like this : (this didn’t work) ?
http://mydomain.com/editfeed.php?host=http://abc.com/rss.aspx?r=45

Thanks you

I am leaving for a few hours and will check back in with you when I return.

But, before I leave, YES, you can use the STRING REPLACE option to change the last ? to something else. Then, inside your PHP file code, you can just change it back. You can replace it with something like, let’s say something unique, like, “<<>>” which would never be placed in a URL. Then, your PHP file code will check the input URL for “<<>>” and if it finds it, it will replace it with a “?”. In this way, you first page will pass something like:
http://mydomain.com/editfeed.php?host=http://abc.com/rss.aspx<<>>r=45
And, the PHP code will strip out the <<>> and get the correct info…
Here is a link to the STRING REPLACE format:
http://php.net/manual/en/function.str-replace.php
Look at the examples posted down the page a little…
That should work for you… Good luck, will be back in 3 hours or so…

Hi ErnieAlex,

Thank you so much for helping me with this problem. It refreshing to know there are people who are so helpful in this world.

I was able to solve the problem, and on the odd chance that someone else is looking for such a solution here it is:

[php]<?php

$host=$_GET['host'];
$url = str_replace("<<<xyz>>>", "?", "$host");
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
$result=preg_replace('|&amp;sublocation=(.*?)</link>|','</link>',$result);
echo $result;

?>[/php]

Hi Again,

I notice another problem with the solution I mentioned above and I wondering if you have a suggestion on how to fix it.

The problem is that with the above solution all characters such as the & symbol get changed to &
and this is correct for the rest of the feed. However, I need the link in between and to have the & symbol and not &

I tired:

$result1=preg_replace(’|&|’,’&’,$result);

but this changes all the & in the feed to & which is not a solution I can use.

I’m stuck on how to change only the data inbetween the and

andy suggestions?

The link between the tags is always in this format: http://www.yourbestdeals.com/deal.aspx?r=test&dealid=2693&location=18

If that helps. (obviously the values change and are not fixed)

Thank you

Well, it appears you can do it this way:

$result1 = str_replace( “&”, “&”, $result);

Not your way with PREG:
$result1=preg_replace(’|&|’,’&’,$result);

That should do it for you…

Thanks for your answer ErineAlex, but this would make the changes to the entire feed.

What I am trying to do is just make these changes in the url between and tags. Any idea how to put in this limitation.

Thank you

No, just use that on the URL string not on the actual XML file.
At least, I thought we were just talking about the URL…
So, pull the data inside the , use str_replace on it only and put it back int the link area…

If not, refresh my memory on what you are trying to change a & to & for…

  • breaking the flow warning *

Sorry to butt in here, but I read the beginning of this thread and just wanted to make a point regarding the original issue. For removing the ‘name’ get string, preg_replace is a perfect choice and will mean one simple line of code instead of exploding and imploding. See below:

[php]$cleaned_url = preg_replace(’/&name=?[^&]*/’, ‘’, $response); // remove &name plus a value (if specified) from $response. Should even work with ww.url.com?a=b&name=john&foo=bar[/php]

My apologies again, just didn’t want you thinking that preg_replace couldn’t help you!

Sponsor our Newsletter | Privacy Policy | Terms of Service