/    Sign up×
Community /Pin to ProfileBookmark

How do I make text into a web link

If i have a page with a web link like ‘www.blah.com’ is there a way I can make it into a link automatically by setting something in the website instead of putting in a <a href… each time?

thanks

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@hastxDec 13.2007 — I've seen pre-built javascripts that will search for any string beginning with "http://" and convert it to a link, but that wont help anybody with JS disabled.

On the server side, it depends how you are getting your "www..." string, for example if you have these strings within a database, you could always append the string with the query to include the "<a href..."
Copy linkTweet thisAlerts:
@tefnutauthorDec 13.2007 — Can it work for both dynamic and non-dynamic content, perhaps something I could put in the top of the page. Im sure I have seen this elsewhere

Thanks
Copy linkTweet thisAlerts:
@SyCoDec 14.2007 — A good way to do this is using ereg_replace (or preg_replace) to recognize a link and add the HTML to make it a link. The hard part is getting the regular expression right however there are lots of libraries out there that should help.

Here's a function by Iasmin Balaj originall posted on PHP.net

http://www.php.net/manual/en/function.preg-replace.php

[code=php]<?php
function hyperlink(&$text)
{
// match protocol://address/path/file.extension?some=variable&another=asf%
$text = preg_replace("/s(([a-zA-Z]+://)([a-z][a-z0-9_..-]*[a-z]{2,6})([a-zA-Z0-9/*-?&%]*))s/i", " <a href="$1">$3</a> ", $text);

// match www.something.domain/path/file.extension?some=variable&another=asf%
$text = preg_replace("/s(www.([a-z][a-z0-9_..-]*[a-z]{2,6})([a-zA-Z0-9/*-?&%]*))s/i", " <a href="http://$1">$2</a> ", $text);

return $text;
}
?>
[/code]

Of course this will only work for dynamic pages I'm not sure it's possible with non dynamic content. Probably with javascript interpreting the output and doing a similar thing before writing it to the page. I've always treated this as a dynamic requirement.
Copy linkTweet thisAlerts:
@tefnutauthorDec 19.2007 — Hi

thanks, but it didnt do anything, i put it in the page
Copy linkTweet thisAlerts:
@SyCoDec 19.2007 — Don't just copy and paste. Go learn how to use it, read the multiple tutorials. Regular expressions are tough to understand but if you want to use them you'll need to learn them. e/preg_replace will do what you want in a dynamic page and you have an example to start you off.

Also what does this tell us other than you have a problem.

"thanks, but it didnt do anything, i put it in the page"

I suppose I could just respond with the same amount of information.
>well you've got an error somewhere.

Not much help huh. If you want help, you'll get it if you post a reasonable request for information. If you want people to write your code for you then I hear good things about the people in Pune, India. Prices are reasonable too.
Copy linkTweet thisAlerts:
@tefnutauthorDec 20.2007 — fair enough
×

Success!

Help @tefnut spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...