[advanced search]
Results from the most recent live auction are here.
22 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Domain Name Industry Newsletter
Go Back   NamePros.Com > Design and Development > Programming
User Name
Password

Old 01-16-2007, 05:54 PM   · #1
w1ww
NamePros Regular
 
w1ww's Avatar
 
Name: Tiago
Location: Mars.
Trader Rating: (15)
Join Date: Oct 2006
Posts: 427
NP$: 79.60 (Donate)
w1ww is a jewel in the roughw1ww is a jewel in the roughw1ww is a jewel in the rough
Extract url [PHP]

Ok,

Sometime ago I've got this code to extract urls from the Google News website:


PHP Code:
<?php

$url
= "http://news.google.com/news/url?sa=T&ct=pt/17-0&fd=R&url=http://www.estadao.com.br/ultimas/nacional/noticias/2006/dez/13/2.htm&cid=1103238007&ei=8mmARcKVMb7maOid1dIO";

preg_match('/&url=(.*?)&cid=/', $url, $result);

echo
$result[1]; // http://www.estadao.com.br/ultimas/nacional/noticias/2006/dez/13/2.htm

?>


I've been trying to adapt it to for example on the url "http://www.google.com" it should result only on "google"

Already tried :
PHP Code:
preg_match('/http://www.(.*?)./', $url, $result);


But I get
Quote:
Warning: preg_match() [function.preg-match]: Unknown modifier '/' in D:\wamp\www\1.php on line 5



So, does anybody has the solution to this little problem?

Thank you,
Tiago


Please register or log-in into NamePros to hide ads
__________________
w1ww is offline   Reply With Quote
Old 01-16-2007, 07:26 PM   · #2
Dan
Buy my domains.
 
Dan's Avatar
 
Name: Dan
Trader Rating: (63)
Join Date: Feb 2006
Posts: 2,801
NP$: 54.00 (Donate)
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
Autism Autism Autism Autism Autism Autism Autism
If you are going to use /'s in your pattern with /'s as your barrier, you need to escape them. Try making it http:\/\/ or using @'s as your barriers.

(You could also just use str_replace to remove the http:// and www.)
Dan is offline   Reply With Quote
Old 01-16-2007, 08:43 PM   · #3
baxter
NamePros Regular
 
baxter's Avatar
 
Trader Rating: (11)
Join Date: Apr 2006
Posts: 278
NP$: 2050.00 (Donate)
baxter is a jewel in the roughbaxter is a jewel in the roughbaxter is a jewel in the rough
Ethan Allen Fund Save The Children
this should work

PHP Code:
preg_match('/\\b(?P<subdomain>(?:[-a-z0-9]+\\.)+)?(?P<domain>(?P<host>[-a-z0-9]+)\\.(?P<tld>[a-z]{2,6}))/', $url, $result));


This will extract multiple subdomains, host ,tld and domain. Hope that helps.

Bax
__________________
Chimps.ca - Swans.ca - Snails.ca
baxter is offline   Reply With Quote
Old 01-17-2007, 02:28 AM   · #4
w1ww
NamePros Regular
 
w1ww's Avatar
 
Name: Tiago
Location: Mars.
Trader Rating: (15)
Join Date: Oct 2006
Posts: 427
NP$: 79.60 (Donate)
w1ww is a jewel in the roughw1ww is a jewel in the roughw1ww is a jewel in the rough
PHP Code:
preg_match('/http:\/\/www.(.*?)./', $url, $result);


returns nothing , just a blank space (tried with the @ too, and it returns blank too!

and
PHP Code:
preg_match('/\\b(?P<subdomain>(?:[-a-z0-9]+\\.)+)?(?P<domain>(?P<host>[-a-z0-9]+)\\.(?P<tld>[a-z]{2,6}))/', $url, $result));


returns
Quote:
Parse error: parse error, unexpected ')' in D:\wamp\www\1.php on line 5




Any suggestion?

Thanks
__________________
w1ww is offline   Reply With Quote
Old 01-17-2007, 04:37 AM   · #5
Dan
Buy my domains.
 
Dan's Avatar
 
Name: Dan
Trader Rating: (63)
Join Date: Feb 2006
Posts: 2,801
NP$: 54.00 (Donate)
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
Autism Autism Autism Autism Autism Autism Autism
PHP Code:
$var = str_replace(array('http://', 'www.'), '', $var);
Dan is offline   Reply With Quote
Old 01-17-2007, 05:20 AM   · #6
Matthew.
Stud Sausage
 
Location: England
Trader Rating: (25)
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (Donate)
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
Adoption Breast Cancer Breast Cancer Cancer Survivorship
Alternatively sticking with the regex:

PHP Code:
$var = 'http://www.google.com';
$var = preg_replace("/(((http|ftp|https):\/\/)|(www\.?))/i", null, $var);

echo
$var; // will output google.com


Just a bit more versitile
__________________
My NamePros Tools
(firefox plugin, google gadget etc)
Matthew. is offline   Reply With Quote
Old 01-17-2007, 07:55 AM   · #7
baxter
NamePros Regular
 
baxter's Avatar
 
Trader Rating: (11)
Join Date: Apr 2006
Posts: 278
NP$: 2050.00 (Donate)
baxter is a jewel in the roughbaxter is a jewel in the roughbaxter is a jewel in the rough
Ethan Allen Fund Save The Children
sorry my mistake I used it in an if statement and left the trailing bracker.

PHP Code:
preg_match('/\\b(?P<subdomain>(?:[-a-z0-9]+\\.)+)?(?P<domain>(?P<host>[-a-z0-9]+)\\.(?P<tld>[a-z]{2,6}))/', $url, $result);
__________________
Chimps.ca - Swans.ca - Snails.ca
baxter is offline   Reply With Quote
Old 01-17-2007, 11:01 AM   · #8
w1ww
NamePros Regular
 
w1ww's Avatar
 
Name: Tiago
Location: Mars.
Trader Rating: (15)
Join Date: Oct 2006
Posts: 427
NP$: 79.60 (Donate)
w1ww is a jewel in the roughw1ww is a jewel in the roughw1ww is a jewel in the rough
Ok,

Didnt explain correctly, for example if the url is 'http://www.google.com/blabalbla.php'

I want it to extract the google part..

Matt, your code does indeed output the google.com but if there is something more after it, it will be outputted too .

baxter, same error as before :\

Thanks for your help till now!
__________________
w1ww is offline   Reply With Quote
Old 01-17-2007, 11:38 AM   · #9
Matthew.
Stud Sausage
 
Location: England
Trader Rating: (25)
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (Donate)
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
Adoption Breast Cancer Breast Cancer Cancer Survivorship
w1ww, how about this:

PHP Code:
$url = 'http://www.google.com/images/';

preg_match('#^(?:http://)(www\.?)?([^/]+)#i', $url , $matches);
// echo $matches[2];
__________________
My NamePros Tools
(firefox plugin, google gadget etc)

Last edited by Matthew. : 01-17-2007 at 11:42 AM.
Matthew. is offline   Reply With Quote
Old 01-17-2007, 12:18 PM   · #10
w1ww
NamePros Regular
 
w1ww's Avatar
 
Name: Tiago
Location: Mars.
Trader Rating: (15)
Join Date: Oct 2006
Posts: 427
NP$: 79.60 (Donate)
w1ww is a jewel in the roughw1ww is a jewel in the roughw1ww is a jewel in the rough
Wow, its working!

I'll test it on my code, it hope it works as I want to ! !

Thank you!!
__________________
w1ww is offline   Reply With Quote
Old 01-17-2007, 02:43 PM   · #11
baxter
NamePros Regular
 
baxter's Avatar
 
Trader Rating: (11)
Join Date: Apr 2006
Posts: 278
NP$: 2050.00 (Donate)
baxter is a jewel in the roughbaxter is a jewel in the roughbaxter is a jewel in the rough
Ethan Allen Fund Save The Children
Sorry I didn't update the first post but the second one work I just checked it.

example:

PHP Code:
<?php                   

$url
= 'http://www.namepros.com/programming/282450-script-selling-script.html';

preg_match('/\\b(?P<subdomain>(?:[-a-z0-9]+\\.)+)?(?P<domain>(?P<host>[-a-z0-9]+)\\.(?P<tld>[a-z]{2,6}))/', $url, $result);

print_r($result);
  
?>


Would ourput

Code:
Array ( [0] => www.namepros.com [subdomain] => www. [1] => www. [domain] => namepros.com [2] => namepros.com [host] => namepros [3] => namepros [tld] => com [4] => com )


Cheers,

Baxter
Originally Posted by w1ww
Ok,

Didnt explain correctly, for example if the url is 'http://www.google.com/blabalbla.php'

I want it to extract the google part..

Matt, your code does indeed output the google.com but if there is something more after it, it will be outputted too .

baxter, same error as before :\

Thanks for your help till now!

__________________
Chimps.ca - Swans.ca - Snails.ca
baxter is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

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
Forum Jump


Site Sponsors
Website Header Design Website Header Design EscrowDNS
Advertise your business at NamePros
All times are GMT -7. The time now is 11:02 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0