如何使用 PHP 函数 preg_replace() 在站点 URL 之后命名 .txt 文件

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

如何在站点 URL 之后命名 txt 文件而不在前面加上 https://http://,如:www.google.com.txt
我想我可能在这里弄错了:fopen($sitenameWithoutHTTP.".txt, "w");

以下是我尝试解决该问题的方式:

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

感谢您帮助找到修复程序。

$arr = ['http','https',':','/','?','&','#','.'];

$sitename = str_replace($arr, '', $sitenameWithoutHTTP);

你也可以使用 base64_encode(), or use parse_url().

$HOST = parse_url($sitenameWithoutHTTP, PHP_URL_HOST);

如果您需要保存真正的 URL 并再次获取它,我认为使用 Md5 哈希的最佳方法

$fileName = md5($sitenameWithoutHTTP).'.txt';

又可以搞到file.php/?getFile2url=[httpLink]

header('Location: '. Md5($_GET['getFile2url']).'.txt');
exit;

希望这可以帮助您实现预期目标!

<?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";
}

上面注释print_r的预期结果应该是:

www.google.com