GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Nov-2002, 16:26
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Regular Expressions question.


For an upcoming article on this site, I was testing out this regex that I came up with to extract urls off a text/string. see script below:
PHP Code:

<?php
$text = 'Add some sample text here with multiple URLs - vB parses them as urls if I type them here.';

extract_urls($text);

function extract_urls( $string )
{
    preg_match_all( '/((http|https|ftp):\/\/|www)([a-z0-9\-\._]+)\/?[a-z0-9_\.\-\?\+\/~=&;,]*/si', $string, $match );
    echo '<p>There are '.sizeof($match[0])." URLs in our text.</p>\n";
    echo "<ol>\n";
    for( $i=0; $i<sizeof($match[0]); $i++ )
    {
        echo '<li>URL is '.$match[0][$i]."</li>\n";
    }
    echo "</ol>\n";
}
?>

crappy vB seems to remove the backslashes in the regex pattern so here it is in code...
Code:
preg_match_all( '/((http|https|ftp):\/\/|www)([a-z0-9\-\._]+)\/?[a-z0-9_\.\-\?\+\/~=&;,]*/si', $string, $match );

the code works quite well except for the fact that it includes the last dot, just after the urls in any test string above. How to get a URL without the punctuation mark at the end of a URL?
  #2  
Old 04-Nov-2002, 03:34
conkermaniac conkermaniac is offline
Member
 
Join Date: Dec 2001
Location: China
Posts: 174
conkermaniac is on a distinguished road
Hi,

I just tested the script out on my computer and I got a result that looked like this:

Quote:
There are 2 URLs in our text.

URL is http://www.aimoo.com/
URL is http://www.freewebspace.net/

Anything wrong with that? I don't see any period.
__________________
You're not supposed to be looking at this.
  #3  
Old 04-Nov-2002, 04:43
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
but what if you tried something like this: (remove the spaces I added in the urls before testing)

PHP Code:

$text = 'Site 1 is w w w . d e s i l v a . b i z, and site 2 is h t t p : 
/ / g i d h e l p . c o m. Also, there are other sites, like w w w . e x a m p l e . c o m.'; 


  #4  
Old 04-Nov-2002, 15:04
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
after much caffeine and chewing on a used nicotine pack... i finally figured it out last night : PCRE Pattern - Extracting URLs
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about loading php into tables or css pcxgamer Web Design Forum 6 17-Sep-2005 22:08
a C input question.. tmike C Programming Language 2 19-Sep-2003 03:39
a C input question tmike C Programming Language 1 16-Sep-2003 03:31
a noobish compiler question Charunks C++ Forum 5 03-Sep-2003 03:18
preg_replace + regular expression EasyExpat MySQL / PHP Forum 4 10-Jul-2003 05:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:41.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.