473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Finding urls and making them hyperlinks

Hello,

I want to take a block of text and add html tags to make hyperlinks
where hyperlink strings exist. So, if I have a variable

$a = "This is http://www.yahoo.com"

I would like to run that through a function such that the value of $a
is

"This is <a href='http://www.yahoo.com'>http://www.yahoo.com</a>"

How can I do this? Any help is greatly appreciated. I am using PHP 4.

Thanks, - Dave

Nov 22 '05 #1
2 1313
On 19 Nov 2005 13:39:46 -0800, la***********@zipmail.com wrote:
I want to take a block of text and add html tags to make hyperlinks
where hyperlink strings exist. So, if I have a variable

$a = "This is http://www.yahoo.com"

I would like to run that through a function such that the value of $a
is

"This is <a href='http://www.yahoo.com'>http://www.yahoo.com</a>"


making the following 2 assumptions:

URL begins with " http" (and there is only one instance within $a)
URL contains no spaces

something like the following (untested) code should work - (this can
most likely be optimized and I'd be interested to know how if anyone
can help out):

<?

$a = "This is http://www.yahoo.com";

// break everything to the left of "http"
$MyURL = substr($a, strpos($a, "http"));
$MyIntro = substr($a, 0, strpos($a, "http"));

// remove anything after URL (if existing)
if (strpos($MyURL," ")) { // there is a space in the string (after
URL)
$MyURL = substr($MyURL, 0, strpos($MyURL, " "));
}

$MyOutput = $MyIntro . "<a href=\"" . $MyURL . "\">" . $MyURL .
"</a>";

echo $MyOutput;
?>
--
John McClumpha
Darkness Reigns - http://www.incitegraphics.com.au/darkness/
Nov 22 '05 #2
Something like this should do the trick:

$a = preg_replace('/(http:\S+)/', '<a href="\1">\1</a>', $a);

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Justin Koivisto | last post by:
OK, I found a thread that help out from a while back (Oct 9, 2002) to give me this pattern: `(((f|ht)tp://)((+)(+)?@)?()+(:\d+)?(\/+)?)`i OK, all is well and good with this until the URL is...
5
by: Mark Rae | last post by:
Hi, I've been asked to write an ASP.NET app which uses traditional hyperlinks with querystrings for navigation e.g. http://<site>/customers/customer.aspx?id=123 The client wants the...
12
by: MARTIN LANNY | last post by:
Hi All, In my program I am loading the content of a text file (main.txt) into a string called 'message'. What I need is to convert all urls and emails in this string into html. This is what...
15
manuelgk
by: manuelgk | last post by:
Hi everybody, I'm a total beginner using Javascript and I would like to know how can I get in a variable the url of hyperlinks? I want to do something like a status bar of mozilla (not a bar) but...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.