Current location: Hot Scripts Forums » Programming Languages » PHP » Help with Regular Expression

Help with Regular Expression

Reply
  #1 (permalink)  
Old 09-26-08, 02:20 AM
coolsaggu's Avatar
coolsaggu coolsaggu is offline
Newbie Coder
 
Join Date: Jun 2008
Location: New Delhi, INDIA
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Help with Regular Expression

Hi all,
I am trying to make up a regular expression.

The purpose of this regular expression will be to scan text and look out for any URL that looks like -

Code:
www.abc.com
abc.com
http://abc.com
http://www.abc.com
and put anchor tags in against these URLs.

The text block can be any long.

Please, any help will be greatly appreciated.
__________________
[ DEVELOPER - Dignity, Energy, Vision, Enthusiasm, Loyalty, Open-minded, Punctuality, Ex-telligence (extra intelligence), Respect ]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-26-08, 08:32 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 2,836
Thanks: 13
Thanked 11 Times in 10 Posts
I think this ought to do it:

(http://)*(www\.)*abc.com
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 09-26-08, 04:08 PM
wirehopper's Avatar
wirehopper wirehopper is offline
Community Liaison
 
Join Date: Feb 2006
Posts: 1,560
Thanks: 2
Thanked 25 Times in 25 Posts
PHP Code:
// List the file as a link, so it can easily be viewed and checked
echo "<a href=\"$url\" target=\"_blank\">$url</a><br />\n";


 
// This is used to extract all the references from src, href, url, action, and window.opens
$ref=run_preg($text,"/(?:(?:src|href|url|action|window.open|popup)+\s*[=\(]+\s*[\"'`]*)([\+\w:?=@&\/#._;-]+)(?:[\s\"'`])/i");

function 
run_preg($text,$pattern)
{
   
// Handles the preg for the scan
   
preg_match_all ($pattern$text$matches);

   return (
is_array($matches)) ? $matches[1]:FALSE;

These are extracts out of robots.spider (http://robots-wizard.com/robots.spider/), download the code at http://robots-wizard.com/robots.spider.tar.gz.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 09-29-08, 07:25 AM
coolsaggu's Avatar
coolsaggu coolsaggu is offline
Newbie Coder
 
Join Date: Jun 2008
Location: New Delhi, INDIA
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Thanx for posting, guys.

While using the method provided by End User of, I devised the following function

PHP Code:
preg_replace('/(http:\/\/)*(www\.)*[a-zA-Z0-9]+\.[a-zA-Z]{3,4}/''<a href="index.php">http://$2$3$4</a>',html_entity_decode($newContent)) 
But the problem still persists, I am not able to catch the string that was replaced. $& doesn't catch either.
__________________
[ DEVELOPER - Dignity, Energy, Vision, Enthusiasm, Loyalty, Open-minded, Punctuality, Ex-telligence (extra intelligence), Respect ]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP regular expression problem coolsaggu PHP 0 08-08-08 03:58 AM
Regular Expression mohit Everything Java 9 05-08-08 05:55 AM
Help with regular expression... :) eddyvlad PHP 2 01-08-06 11:10 PM
regular expression for parsing url link musmanm80 Perl 1 07-28-05 12:58 AM
suggest a regular expression gmadhukarreddy Perl 2 01-08-04 01:56 AM


All times are GMT -5. The time now is 04:33 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.