Log in

View Full Version : Strip/replace url part?



william james
01-26-2011, 11:08 PM
Hello,

I have a url from my site A that I want to load into a href= tag of my site B


url from site A= http://domain.com/5234566/site A.php

I want only the 5234566 to be load into my href on site B


<A HREF="site B.php?id=5234566">SITE B</A>

Is this possible?

Thank you in advance!

djr33
01-26-2011, 11:15 PM
You can use string functions like strpos() and substr(), or you could use regex, which is more complicated but more powerful.

The main question is how exactly you can determine where that string will be in the URL. It may seem simple for you to find it, but you will need to give the computer very specific instructions. Is it always the first element after the domain name? Does it always start with 5? Does it only contain numbers? Maybe the only way you can recognize it is that it will be the only string in it that is exactly 7 numbers long, without any other characters. But whatever you do, make sure it isn't possible that there are any variations or duplicates or you might get unexpected results.

A way to determine how to approach this sort of problem is this: how consistent is the format? If you get a URL that looks almost exactly like that every time, it's easy. If there are many ways it can differ, it's a lot more complex.

If you have trouble, you can post some examples of what the URLs will look like and please include all possible variations (not all possible URLs, but just the format variations).

If the format is relatively similar all the time, you could try this as an easy method:
http://php.net/manual/en/function.parse-url.php

Moglizorz
01-28-2011, 07:47 PM
Use php explode then grab the fourth part of the array (I think it's the fourth, some trial and error will find out):

http://php.net/manual/en/function.explode.php


If that's what you mean. Maybe expand a bit more on what exactly you want.