Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2002, 06:30 AM   PM User | #1
Jabbamonkey
New Coder

 
Join Date: Aug 2002
Location: New York, NY
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Jabbamonkey is an unknown quantity at this point
Question URLs and TEXT fields

Ok, I have a database with the following text field...

"This event has been going on for the past 20 years. For more information, visit www.domainname.com or call 212.555.1234."

Well, when I print this field on a php page, I'm interested in making all urls a link. So, I'm looking to substitute any...

www.domainname.com OR http://www.domainname.com

with

<a href="http://www.domainname.com/" target="_blank">www.domainname.com</a>

In short, I'm trying to turn the above text field into:

"This event has been going on for the past 20 years. For more information, visit <a href="http://www.domainname.com/" target="_blank">www.domainname.com</a> or call 212.555.1234."

How do you go about changing elements inside a text field? I know that I could put the html in the text field, but the administrator of the database will not want to deal with that. Any ideas?

Jabbamonkey
Jabbamonkey is offline   Reply With Quote
Old 08-10-2002, 11:24 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,901
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
regular expressions are required, here in a function...

PHP Code:
<?
function parse_urls($str){
    
$str=eregi_replace("([ \r\n])www\\.([^ ,\r\n]*)"," <a href=\"http://www.\\2\">\\2</a>",$str);
    
$str=eregi_replace("^www\\.([^ ,\r\n]*)","<a href=\"http://www.\\1\">\\1</a>",$str);
    
$str=eregi_replace("([ \r\n])http://([^ ,\r\n]*)"," <a href=\"http://\\2\">\\2</a>",$str);
    
$str=eregi_replace("^http://([^ ,\r\n]*)"," <a href=\"http://\\1\">\\1</a>",$str);
    return 
$str;
}

echo 
parse_urls('www.this.com and now http://www.that.com');
?>
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 08-10-2002, 03:13 PM   PM User | #3
Jabbamonkey
New Coder

 
Join Date: Aug 2002
Location: New York, NY
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Jabbamonkey is an unknown quantity at this point
Arrow Regualr Expressions

Wow, thanks. I've never used regular expressions before (very new to php). Can you explain what regular expressions are, and what they do (or provide a link to where I can find out).

I'm the type that doesn't just like to cut and paste, but also wants to learn how it works. Thanks.

Jabbamonkey
Jabbamonkey is offline   Reply With Quote
Old 10-24-2004, 04:11 AM   PM User | #4
SpiritualStorms
Regular Coder

 
Join Date: Dec 2003
Location: America
Posts: 544
Thanks: 0
Thanked 0 Times in 0 Posts
SpiritualStorms is an unknown quantity at this point
i am not sure in what specific language you are interested in work with, but if you are interested in some tutorials, you could always type something like,

"tutorials regular expressions + javascript".

or

"tutorials regular expressions + PHP",

into a search box, and then hit search, for the browser to have its results. I found the following link useful, if you are interested:

http://search.yahoo.com/search?fr=sl...ar+expressions

You might want to go to Amazon.com, and get type "regular expressions" into their search boxes, under the heading of books, and you should be able to find a book, or 2, on the subject matter.

Good luck.
__________________
LovesWar
SpiritualStorms is offline   Reply With Quote
Old 10-24-2004, 05:22 AM   PM User | #5
titansolaris
New Coder

 
Join Date: Jun 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
titansolaris is an unknown quantity at this point
http://codingforums.com/showthread.php?t=46511 Look here!
titansolaris is offline   Reply With Quote
Old 10-24-2004, 03:07 PM   PM User | #6
trib4lmaniac
Regular Coder

 
trib4lmaniac's Avatar
 
Join Date: Feb 2004
Location: Cornwall, UK
Posts: 535
Thanks: 0
Thanked 0 Times in 0 Posts
trib4lmaniac is an unknown quantity at this point
www.regularexpressions.info
trib4lmaniac is offline   Reply With Quote
Old 10-26-2004, 12:23 AM   PM User | #7
greg76
New to the CF scene

 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
greg76 is an unknown quantity at this point
Arrow

I have similar to Firepages' solution.

PHP Code:
function addLinks($var$linkstyle){
    
$var ereg_replace("([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])""<a class=\"$linkstyle\" href=\"\\1\">\\1</a>"$var);
    
$var ereg_replace("([_a-zA-z0-9\-]+(\.[_a-zA-z0-9\-]+)*\@" "[_a-zA-z0-9\-]+(\.[a-zA-z]{1,3})+)""<a class=\"$linkstyle\" href=\"mailto:\\1\">\\1</a>"$var);
return 
$var

whatever you type in your cms (I am assuming you are using some cms) http://www.link.com or email@email.com, those will be turned to links.

When you want to edit text again, use this function before displaying want-to-edit data:

PHP Code:
function remLinks($var$linkstyle){
    
$var ereg_replace ("(<a class=\"$linkstyle\" href=\"[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]\">)([[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/])(</a>)","\\2"$var);
    
$var ereg_replace ("(<a class=\"$linkstyle\" href=\"mailto:)([_a-zA-z0-9\-]+(\.[_a-zA-z0-9\-]+)*\@" "[_a-zA-z0-9\-]+(\.[a-zA-z]{1,3})+)(\">)([_a-zA-z0-9\-]+(\.[_a-zA-z0-9\-]+)*\@" "[_a-zA-z0-9\-]+(\.[a-zA-z]{1,3})+</a>)""\\2",  $var);
return 
$var


I copied and modified above from my old CMS, so I hope it works properly. If not - give me a yel.

~Greg
greg76 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:26 PM.


Advertisement
Log in to turn off these ads.