volatileboy Posted January 5, 2011 Share Posted January 5, 2011 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 https://forums.phpfreaks.com/topic/223499-matches-being-weird/ Share on other sites More sharing options...
volatileboy Posted January 5, 2011 Author Share Posted January 5, 2011 Nevermind I solved it, I was using square brackets around the domains. The expression may not be sufficient still though, if anyone has any flaws to the above method that I should know about then please let me know. Thanks! Link to comment https://forums.phpfreaks.com/topic/223499-matches-being-weird/#findComment-1155289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.