Mjhd Mjhd - 4 months ago 11
PHP Question

How do I use PHP Function preg_replace() in naming a .txt file after a site URL

How can I name a txt file after a site URL without the preceding

https://
or
http://
as in:
www.google.com.txt
?

I think I might be getting it wrong here:
fopen($sitenameWithoutHTTP.".txt, "w");


Below is the way I'm trying to address that:

<?php
//
@mkdir("result", 0755);
@chdir("result");
$link = $sitename;
$sitename = preg_replace('#^https?://#', '', $sitenameWithoutHTTP);
$resultfile = fopen($sitenameWithoutHTTP.".txt", "w");
//
?>


Thanks for helping find a fix.

Answer

Hope this helps you achieve what you intended!

<?php
$siteName = 'https://www.google.com';

$siteNameWithoutHttps = preg_replace('#^https?://#', '', $siteName);

// print_r($siteNameWithoutHttps);

$resultFile = fopen($siteNameWithoutHttps.".txt", "w");

// run a check
if($resultFile == true) {
echo "success";
} else {
echo "failed";
}

The expected result for the commented print_r above should be:

www.google.com
Comments