[PHP HELP] Cleaning after .com on URL

Ramazan

Regular Member
Joined
Aug 19, 2018
Messages
441
Reaction score
310
I have example url : https://www.x-on.com.au/Blog/post/2020/09/03/linear-voltage-regulator
I need to clean after .com or first / with code. I have some codes it's can looks bad but works.
I made an function with replace
PHP:
function urlcleaner($data ) {
    $data = str_replace(".", "" , $data);
    $data = str_replace("www", "" , $data);
    $data = str_replace("www.", "" , $data);
    $data = str_replace(".com", "" , $data);
    $data = str_replace("http://", "" , $data);
    $data = str_replace("/", "" , $data);
    $data = str_replace("//", "" , $data);
    $data = str_replace("://", "" , $data);
    $data = str_replace("https://", "" , $data);
    $data = str_replace(".org", "" , $data);
    $data = str_replace(".vn", "" , $data);
    $data = str_replace(".au", "" , $data);
    $data = str_replace(".at", "" , $data);
    $data = str_replace(".eu", "" , $data);
    $data = str_replace(".de", "" , $data);
    $data = str_replace(".uk", "" , $data);
    $data = str_replace(".co", "" , $data);
    $data = str_replace(".aspx", "" , $data);
    $data = str_replace(".php", "" , $data);
    $data = str_replace(".html", "" , $data);
    $data = str_replace(".css", "" , $data);
    $data = str_replace("-", "" , $data);
    return $data;
}
echo "<pre/>";
print_r( array_map( 'strtolower' ,urlcleaner($file))  )

Cleaning URL things only need websitename instead of websitename.com/blabla/bla

I hope I'm clean explained. Any idea? Maybe with that code I can clean before sitename. My coding skills not well as you see :D
 
Lol
Why not use parse_url() for the entire code.

Example:
PHP:
$url = 'https://fincoapps.com/common-errors-and-exceptions-faced-by-android-developers/';
$parsedURL = parse_url($url);

// should return fincoapps.com
echo $parsedURL['host'];

The code should replace everything you wrote in the urlcleaner() function
 
Lol
Why not use parse_url() for the entire code.

Example:
PHP:
$url = 'https://fincoapps.com/common-errors-and-exceptions-faced-by-android-developers/';
$parsedURL = parse_url($url);
echo $parsedURL['host']; // should return 'fincoapps.com'

The code should replace everything you wrote in the urlcleaner() function
I just found that function now with tom reply. Trying to use now thank you too!
 
Okay, thanks to everyone I made it with the parse URL function. My urlcleaner function has unnecessary things but it's fine maybe someone need it.
PHP:
function urlcleaner($data ) {
    $data = str_replace(".", "" , $data);
    $data = str_replace("www", "" , $data);
    $data = str_replace("www.", "" , $data);
    $data = str_replace(".com", "" , $data);
    $data = str_replace("http://", "" , $data);
    $data = str_replace("/", "" , $data);
    $data = str_replace("//", "" , $data);
    $data = str_replace("://", "" , $data);
    $data = str_replace("https://", "" , $data);
    $data = str_replace(".org", "" , $data);
    $data = str_replace(".vn", "" , $data);
    $data = str_replace(".au", "" , $data);
    $data = str_replace(".at", "" , $data);
    $data = str_replace(".eu", "" , $data);
    $data = str_replace(".de", "" , $data);
    $data = str_replace(".uk", "" , $data);
    $data = str_replace(".co", "" , $data);
    $data = str_replace(".aspx", "" , $data);
    $data = str_replace(".php", "" , $data);
    $data = str_replace(".html", "" , $data);
    $data = str_replace(".css", "" , $data);
    $data = str_replace("-", "" , $data);
    $data = str_replace(".org", "" , $data);
    $data = str_replace("com", "" , $data);
    $data = str_replace("org", "" , $data);
    $data = str_replace("Www", "" , $data);
    $data = str_replace("WWW", "" , $data);
    $data = str_replace("WwW", "" , $data);
    return $data;
}

foreach ($file as $url) {
    $uncleaned = parse_url($url);
    echo "<pre/>";
    echo urlcleaner( $uncleaned['host']) ;
  // echo  $uncleaned['host'];
}
 
Back
Top