Jump to content

Matches being weird!


volatileboy

Recommended Posts

Hey peeps got a quick question, basically I am trying to strip the domain part from a url so the bit I am after would be "domain.com", I have wrote an expression to match it and it seems to work (havent thoroughly tested it).

 

When I output the $matches variable when testing on "http://www.yahoo.com" it only outputs www.y which I cant understand, any help is appreciated.

 

Here is the code I am using:

 

$domains = 'com|net|org|me|co|info|mobi|tv|us|biz|cm|co.uk|cc|ca|eu|tel|nu|in|aaa.pro|aca.pro|acct.pro|asia|avocat.pro|bz|ws|ac|am|at|bar.pro|be|br.com|cn|cn.com|co.nz|com.cn|com.co|com.mx|com.tw|cpa.pro|de|de.com|eng.pro|eu.com|fm|gs|hu.com|idv.tw|it|jp|jur.pro|kids.us|la|law.pro|me.uk|med.pro|ms|name|net.cn|net.co|nl|no.com|nom.co|org.cn|org.nz|org.tw|org.uk|pro|qc.com|recht.pro|ru.com|sa.com|se.com|se.net|sh|tc|tm|tw|uk.com|uk.net|us.com|uy.com|vg|za.com';
$domains = str_replace(".", "\.", $domains);

$pattern = '/[a-zA-Z0-9]{2,100}\.[' . $domains . ']/';

$test1 = "http://www.yahoo.com";
if(preg_match($pattern, $test1, $matches)) {
      echo 'Test 1 is okay<br />';

      echo $matches[0] . '<br />' . $matches[1] . '<br />' . $matches[0][0] . '<br />' . $matches[0][1] . '<Br />' . $matches[1][0] . '<br />' . $matches[1][1] . '<br />' . $matches[2][0] . '<br />' . $matches[2][1] . '<br />' . $matches[2][2];
}

This is my test code, it output the following:

 

Test 1 is okay
1
www.y

w
w


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.