 |
Results from the most recent live auction are here.
22 members in the live chat room. Join Chat!
| |
01-16-2007, 05:54 PM
|
· #1 | | NamePros Regular Name: Tiago Location: Mars. Join Date: Oct 2006
Posts: 427
NP$: 79.60 ( Donate)
| 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
So, does anybody has the solution to this little problem?
Thank you,
Tiago
__________________ |
| |
01-16-2007, 07:26 PM
|
· #2 | | Buy my domains. Name: Dan Join Date: Feb 2006
Posts: 2,801
NP$: 54.00 ( Donate)
| 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.) |
| |
01-16-2007, 08:43 PM
|
· #3 | | NamePros Regular Join Date: Apr 2006
Posts: 278
NP$: 2050.00 ( Donate)
| 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 |
| |
01-17-2007, 02:28 AM
|
· #4 | | NamePros Regular Name: Tiago Location: Mars. Join Date: Oct 2006
Posts: 427
NP$: 79.60 ( Donate)
| 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
Any suggestion?
Thanks
__________________ |
| |
01-17-2007, 04:37 AM
|
· #5 | | Buy my domains. Name: Dan Join Date: Feb 2006
Posts: 2,801
NP$: 54.00 ( Donate)
| PHP Code: $var = str_replace(array('http://', 'www.'), '', $var);
|
| |
01-17-2007, 05:20 AM
|
· #6 | | Stud Sausage Location: England Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 ( Donate)
| 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  |
| |
01-17-2007, 07:55 AM
|
· #7 | | NamePros Regular Join Date: Apr 2006
Posts: 278
NP$: 2050.00 ( Donate)
| 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 |
| |
01-17-2007, 11:01 AM
|
· #8 | | NamePros Regular Name: Tiago Location: Mars. Join Date: Oct 2006
Posts: 427
NP$: 79.60 ( Donate)
| 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!
__________________ |
| |
01-17-2007, 11:38 AM
|
· #9 | | Stud Sausage Location: England Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 ( Donate)
| w1ww, how about this: PHP Code: $url = 'http://www.google.com/images/';
preg_match('#^(?:http://)(www\.?)?([^/]+)#i', $url , $matches);
// echo $matches[2];
Last edited by Matthew. : 01-17-2007 at 11:42 AM.
|
| |
01-17-2007, 12:18 PM
|
· #10 | | NamePros Regular Name: Tiago Location: Mars. Join Date: Oct 2006
Posts: 427
NP$: 79.60 ( Donate)
| Wow, its working!
I'll test it on my code, it hope it works as I want to  ! !
Thank you!!
__________________ |
| |
01-17-2007, 02:43 PM
|
· #11 | | NamePros Regular Join Date: Apr 2006
Posts: 278
NP$: 2050.00 ( Donate)
| 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
__________________ Chimps.ca - Swans.ca - Snails.ca |
| |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |