0

I have a long list with website address's. But i have to filter them and get only the part "abcd". In my cut/paste/algorithm i have very random format's of websites to deal, and dealing with large list is like very time consuming.

Example:

www.abcd.tld.tld.tld to abcd
http://www.abcd.tld.tdl to abcd
abcd.tld to abcd
abcd.tld.tld to abcd
http://abcd.tld to abcd
http://abcd.tld.tld to abcd

Which Zend_Filter i can ues tot cut front and tail, and always get the middle part of "abcd". Or is there any PHP built-in function which can do this?

5
  • 2
    The part to extract looks completely random to me. You might want to clarify the rules by which to extract.
    – Gordon
    Feb 7, 2012 at 18:56
  • Yes, that is the problem i got. I did split on DOT but because its so random i can not put that as final algorithm.
    – user285594
    Feb 7, 2012 at 18:58
  • 1
    Hm, and http://http.abcd.tld.tld. http://static.abcd.tld. or http://totalawesomerandomness.abcd.tld?
    – Wrikken
    Feb 7, 2012 at 18:59
  • @Wrikken: from your samples i have to http for 1st one, for second one i have to get static and for the last one i have to get totalawesomerandomness + i have also some URL listed without http:// or www. or http://www.
    – user285594
    Feb 7, 2012 at 19:03
  • 1
    You mean to strip out ccSLD's too, then the only thing I can think of is indeed listing all of those. Good luck, Brazil for instance seems to have 67
    – Wrikken
    Feb 7, 2012 at 19:13

2 Answers 2

0

You could do this with some basic string functions in PHP. Load all your URLs into a string variable and then do a simple str_replace

$old_urls; // load your urls into this variable
$search = array('http://','https://','www.','.com','.net','.us','.org','.edu','.us'); // etc, add more tlds
$new_urls = str_replace($search,'',$old_urls);

Would this work for you?

2
  • OK - that logic is fine. But there are many other sites i got which has .tld.tld. So what you are saying is collect all those TLD and make that array?
    – user285594
    Feb 7, 2012 at 19:01
  • 1
    Certainly, you can expand on the $search array in the code by adding more elements ...,'.tld','.tld2'... to the array. It's definitely a down and dirty solution to what you are looking to do, but it should work just fine after you place all the '.tld's you are working with.
    – Markus
    Feb 7, 2012 at 19:24
0

preg_match('_https?://([a-z0-9-])\..*_i', $original_url, $matches); should do you well. $matches[1] should now contain the first section after http(s):// and before the first ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.