Question

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?

Was it helpful?

Solution

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?

OTHER TIPS

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 ..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top