TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 08-14-2008, 01:27 PM   #1 (permalink)
The Contributor
 
localhost's Avatar
 
Join Date: Apr 2007
Location: Hampshire
Posts: 28
Thanks: 1
localhost is on a distinguished road
Default Parsing URL in text

Hello,

I have paragraphs of text in a database, and I am trying to convert the url's to proper links, e.g. :
This site Google is cool
would be : This site Google is cool

and the same if they had http:// on.

So far my code isn't working, so please I would be grateful if someone could help me achieve above.

Many Thanks,

Steven Gibbons

PHP Code:
if(!empty($data)) {
$url mysql_real_escape_string($_POST['url']);
$text preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@''<a href="$1">$1</a>'$url);
echo 
$text;

__________________
Send a message via MSN to localhost Send a message via Skype™ to localhost
localhost is offline  
Reply With Quote
Old 08-14-2008, 02:35 PM   #2 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

PHP Code:
$this->_string preg_replace('~((https?|ftp)\://[a-z0-9+_-]+(\.[a-z0-9+_-]+)*(/([a-z0-9+_-]\.?)+)*/?(\?[a-z+&_.-][a-z0-9;:@/&%=+$_.-]*)?(#[a-z_.-][a-z0-9+_.-]*)?)~is''<a href="$1">$1</a>'$this->_string); 
Has been working for me... doesn't parse the URLs title like vBulletins parser, but I still think it's a good start. Looks for web or ftp protocols, handles top level and sub-domains with ease, with any number of sub-directories, a query string, or an anchor.
-m
delayedinsanity is offline  
Reply With Quote
Old 08-14-2008, 02:42 PM   #3 (permalink)
The Contributor
 
localhost's Avatar
 
Join Date: Apr 2007
Location: Hampshire
Posts: 28
Thanks: 1
localhost is on a distinguished road
Default

Thanks for your reply. I will give it a go.
__________________
Send a message via MSN to localhost Send a message via Skype™ to localhost
localhost is offline  
Reply With Quote
Old 08-14-2008, 02:47 PM   #4 (permalink)
The Contributor
 
localhost's Avatar
 
Join Date: Apr 2007
Location: Hampshire
Posts: 28
Thanks: 1
localhost is on a distinguished road
Default

Thanks for your reply. The only problem I am having now is it only works with http:// not just www.

Thanks,

Steve
__________________
Send a message via MSN to localhost Send a message via Skype™ to localhost
localhost is offline  
Reply With Quote
Old 08-14-2008, 06:51 PM   #5 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

Well, I suppose you could do this:

PHP Code:
$string preg_replace('~(((https?|ftp)\://)?(www\.)?[a-z0-9+_-]+(\.[a-z0-9+_-]+)*(/([a-z0-9+_-]\.?)+)*/?(\?[a-z+&_.-][a-z0-9;:@/&%=+$_.-]*)?(#[a-z_.-][a-z0-9+_.-]*)?)~is''<a href="$1">$1</a>'$string); 
I would actually consider this a fairly good regular expression to learn from, because although it looks complex to those who are new or completely foreign to regexps, it's actually a really easy regexp to break down once you start learning how they work - probably just as easy to modify and optimize as well, I just put it together in a rather quick fashion a while back and haven't touched it since.

If you're interested;

TalkPHP - Search Results
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
Regular Expression Library
Regular Expressions Cheat Sheet (V2) - Cheat Sheets - Added Bytes
-m
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:46 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0