php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #16642 replace url with a link
Submitted: 2002-04-16 13:27 UTC Modified: 2002-04-16 13:50 UTC
From: andreas dot schempp at gmx dot ch Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.1.2 OS: Linux 2.4.17 (self compiled)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andreas dot schempp at gmx dot ch
New email:
PHP Version: OS:

 

 [2002-04-16 13:27 UTC] andreas dot schempp at gmx dot ch
I could not find any function to replace an url within a string with a link

if someone writes http://www.php.net, I want I to be replaced with <a href="http://www.php.net>

I saw this on your page too, its easy to recreate your function myself with 

$string = preg_replace("((http://)([a-zA-Z1-9\-_\.]+\.[a-zA-Z1-9\-_]+\.[a-z]{2,3}(/[a-z\-/\._\?=&]+)?))", "<a href=\"\\1\\2\" target=\"_blank\">\\0</a>", $string);

but wouldnt it be nice to replace an url like www.php.net too?

For now, I use

$string = preg_replace("((http://|ftp://)?([a-zA-Z1-9\-_\.]+\.[a-zA-Z1-9\-_]+\.[a-z]{2,3}(/[a-z\-/\._\?=&]+)?))", "<a href=\"\\1\\2\" target=\"_blank\">\\0</a>", $string);

to convert every url to an link.
but if you have an url like www.php.net, it will create a link like <a href="www.php.net">, wich will not work.
To prevent this, I use

$string = preg_replace("((href=\")([a-zA-Z1-9\-_\.]+\.[a-zA-Z1-9\-_]+\.[a-z]{2,3}(/[a-z\-/\._\?=&]+)?))", "href=\"http://\\2", $string);

wich will replace anything like href="something.php.net with href="http://something.php.net

I think it will be nice for other people to include (something like) this function in php, because many people would use it

thanks

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-16 13:31 UTC] sander@php.net
No, we can't include a function for stuff like this. This can be easily done in PHP, so there's no need for a native function.
 [2002-04-16 13:41 UTC] robert at webmotion dot com
The same could be said of addSlashes, htmlspecialchars, htmlentities and
a multitude of other functions. I find your argument lacking.

Cheers,
Rob.
 [2002-04-16 13:50 UTC] andreas dot schempp at gmx dot ch
Its not as simple as you think, specialy for non-preg-users.
I could not find any real documentation wich arguments could be used for preg_replace, only the ones at ereg_replace, posted from a user.
I needed half a week to construct the things you see here...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 06 18:01:35 2024 UTC