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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding a string to an existing url

Status
Not open for further replies.

cellpeter

Programmer
Dec 30, 2004
34
US
ok what am trying to do is create a conveter that takes out all tags from a website and only display text and the links.

My problem however is finding a way to add a string to an already existing url while the converter is translating

example 1:

unmodified url = <a href=
to

modified url = <a href=example.php?url=http://www.google.com</a>

when using clicks the link the browser will go to google

now i've figured out how to do example 1 by using str_replace however i can't use the str_replace method when the url isn't absolute

example 2

<a href=whatever.php>what</a> to <a href=example.php?url=whatever.html>what</a>

when user clicks browser will not go anywhere...

now i am asking if anyone can help me find a way to add the full path with relative link so that when a user clicks on the link the browser will go to it's intended destination..
i've tried the <base href= method but it doesn't work as attended

thank you for any replies and apologies for this long message :)
 
Something like this?
Code:
$str = "Go to google <a href="[URL unfurl="true"]http://www.google.com">here</a>!";[/URL]

$modified = preg_replace(/<a href="([^"]*)">/, '<a href="example.php?url=' . $1 . '">', $str);

echo $str;
echo $modified; // should print correctly, haven't tested though...

--Chessbot

"In that blessed region of Four Dimensions, shall we linger on the threshold of the Fifth, and not enter therein? Ah, no! [...] Then, yielding to our intellectual onset, the gates of the Sixth Dimension shall fly open; after that a Seventh, and then an Eighth -- --" Flatland, A. Square (E. A. Abbott)
 
thanks for the reply, but sorry to say that there was an error in line 2 but i get your solution and it works somewhat but what am looking for is a way to replace the relative link with an absolute link so that the converter can translate the link

like for instance if the user browsed a website called
and there were links such as <a href=news.html>go to news</a> i would like the converter to add the absolute link

<a href=example.php?url=http://www.example.com/news.html>go to news</a>

thanks for the suggestions so far :)
 
yeah sorry if i wasn't clear yeah what i want is to make goto.php go to site which is in the querystring like you said

however my problem is that whenever the link specified is not absolute e.g. news.html and not
the goto.php will not be able to go to the site in the querystring because the link is relative

what i would like is to find a way to add the full absolute
path to the link if the link is relative

for example

from <a href=goto.php?url=news.html>news</a>

to
<a href=goto.php?url=http://www.cnn.com/news.html>news</a>

maybe a what if system will work?

e.g.

if(link has http:// AKA absolute)
{
then do code
}
if(link has no http:// AKA relative}
{
then do code
}

thanks for the replies so far
 
hmm..
I think this is easy..
You want your script to either _goto_ a local page, or to a remote page?

I however have thought of a better redirect script for you.. it's a bit more trickier to make though..

You'll need a file called redirect, without extension.. (no .php).

Then, you need .htaccess to rewrite it to run redirect as PHP default. You also need some code that fixes with querystring, etc.

The general idea, is that you then can have:

yourdomain.com/redirect/
then, the redirect file (ITS NOT A FOLDER), will gather the variable and redirect to it (header location, with 301, permanently moved).

You can search the url then, for www, http, or whatever. You can explode it on / and strip away the domain..

for local files, you can call the script like:
yourdomain.com/localhost/path/to/file.html

you then explode, like above.. strip away yourdomain.com/ before exploding.

if first element is localhost, it's a local file, else, it's remote..

ps. I can provide some code samples tomorrow..

Olav Alexander Mjelde
Admin & Webmaster
 
hmm thank you so much for your solution

about my method being so advanced lol well to be honest i just started learning php a few months ago so i just based my so called solution on what i know so far

i would really appreciate it if you can display some examples

thank you very much


 
sorry, I know I promised you samples today, but I'll have to do it tomorrow..

Have to go to bed soon, since I have 8 hrs work tomorrow :( I have to extract some code of something I'm currently programming for usage in a custom CMS I'm developing.

Olav Alexander Mjelde
Admin & Webmaster
 
ok, I have some code samples here.

file: '.htaccess'
Code:
<files ~ "^\.ht">
	Order allow,deny
	Deny from all
</files>

<Files redirect>
	ForceType application/x-httpd-php
</Files>

Then, you need a file called site, with no extension..
the .htaccess will force it to be run as php!

Then, there is a script for "google friendly urls":

That code, will make you able to run urls like:
or:
If you want www, you could do:

Then you can SWITCH this, to determine if it's local or remote.. eg. localhost == local, else == remote.
You then use header with location, and 301, permanently moved.

This will make your redirect script google-friendly, rather than having: redirect.php?out=2

ps. there is also a "part II" of the script on evolt.org, but I was unable to find it just now..
I'll look some more later today, if/when I have the time.

if you need code-samples on header location, with 301:
Code:
header("HTTP/1.1 301 Moved Permanently"); 
header("location: [URL unfurl="true"]http://www.domain.tld/");[/URL] 
header("Connection: close");

Remember: If you use local redirection (localhost), use is_file()

eg. if !is_file(), do not redirect, but tell user that the page/file is not found.

something like:

1. user goes to 2. script looks for /file.html (since it's localhost)
3. file is/is not found
4. site redirects/does not redirect

You could also make a mysql table, and run a hit-counter.
There is also a possibility for you to make a "hits-in" counter.. (check referrer on localhost redirections, if they are != localhost, store them as redirections to the redirect script)

I hope you get what I'm saying.
If you fool around with the script on evolt.org, you'll see it's truly very simple..

instead of including the exploded arrya, you will redirect to it. You will need to put some logic there, ofcourse.

Good luck!


Olav Alexander Mjelde
Admin & Webmaster
 
ah, sorry.. I did a typo above.

<Files redirect>

This means the file with no extension has to be called 'redirect' (without the '').

if you wish to call it something else, just change in the .htaccess

if you cannot see the .htaccess in your ftp client, try -u

Olav Alexander Mjelde
Admin & Webmaster
 
thank you so much for your solution DaButcher it is working just fine

thanks once again :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top