doulu4534 2016-07-30 15:44
浏览 20
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • dsvf46980 2016-07-30 16:22
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站