doujie3888 2017-01-31 17:55
浏览 34
已采纳

在某些情况下更改HTML文件[关闭]

I have various html files in which I need to fill 3 Code snippets at 3 different positions. once directly after head starts, once in beginning and once in the end of the body.

I know where those html files are, cause I have a database for some other reasons, where I can also track down the html-files.

So I can run my php, getting all the files from database, then I need to check if my codesnippets are already existing in each file, if they do I do nothing, if not I need to add the snippets.

Even so php might not be the best choice, I do not have the time for now to learn something new (even though I appreciate tips, with which language this would work best for later)

I checked similar posts, but I can´t find the answer i´m seeking for

  • 写回答

1条回答 默认 最新

  • douhui0975 2017-01-31 21:24
    关注

    First, it's helpful to distinguish between a "file" and a "filename." When you say that you will be "getting all the files from database," I assume you mean that you'll be getting the filenames from the database.

    If so, you can use the PHP "file_get_contents" function to read the file contents into a string. Then you can search the string (using various string functions such as "strpos" or "stripos") to see if it contains the HTML snippet you're looking for.

    If the HTML snipped you're looking for isn't there, you can modify the string (again using various string functions) to add it. Search the PHP documentation for "string functions" since there are different ways to go about doing this.

    When you're done you can write the altered string back to file using the "file_put_contents" function.

    I won't go into the function syntax since you will, of course, want to research that in the PHP documentation regardless of what you read here.

    There may be other problems with file permissions. If you get errors that you can't write to a file, you'll need to look into the chmod function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊