473,803 Members | 2,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1356
On 19 Nov 2005 13:39:46 -0800, la***********@z ipmail.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
4339
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 used at the end of a sentance. I am assuming that I will need a negative lookahead somehow, but I just can't wrap my mind around this one... Want to match only the URL...
5
1200
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 querystrings to be encrypted, sort of like Amazon.com does. That's fine - no problem there. However, the client now asks if there is any way of detecting whether the
12
1689
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 I did to convert hard returns to <br>'s and it works fine: Dim sws As New IO.StreamWriter(pathtosave)
15
2811
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 just get the urls no matter the extension of the hyperlinks (.html, .php, .pdf, etc). So please I need your help :) . Thanks
0
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.