I'm cleaning URL from a text file with a PHP script, here's the code right now :
$file = __DIR__."/url.txt";
$f = fopen($file, "r");
$array1 = array();
while ( $line = fgets($f, 1000) ) {
$nl = mb_strtolower($line,'UTF-8');
$array1[] = $nl;
}
foreach ($array1 as $value) {
$value = preg_replace('#^https?://#', '', $value);
$value = preg_replace('#^www.#', '', $value);
echo $value."<br>";
}
So I remove the http://
and www
from these urls
Here's the output :
urlnumberone.com
urlnumbertwo.uk
subdomain.urlnumberthree.com
urlnumberfour.com
What I want is to remove subdomain too, and just have urlnumberthree.com
Thanks for your help !
Pure regex solution:
preg_match('#[^\.]+[\.]{1}[^\.]+$#', $value , $matches);
$value = $matches[0];
This replaces both of your preg_replace
s.
Check occurrences of '.' if they are more than 1, remove the beginning until first dot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.