Mjhd Mjhd - 2 years ago 75
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

as in:

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

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

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

Thanks for helping find a fix.

Answer Source

Hope this helps you achieve what you intended!

$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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download