首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >php案例:文本内容查找替换功能

php案例:文本内容查找替换功能

作者头像
贵哥的编程之路
发布2022-09-23 21:03:25
发布2022-09-23 21:03:25
3.3K0
举报

作者:陈业贵 华为云享专家 51cto(专家博主 明日之星 TOP红人) 阿里云专家博主

文章目录


cyg.php

代码语言:javascript
复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> 文本内容查找替换 </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="author">
  <meta name="Keywords" content="cstp">
  <meta name="Description" content="no-description">
  <meta http-equiv="Content-type" content="text/html;charset=GB2312">
<link rel='stylesheet' type='text/css' href='style.css' />
 </head>
<?

?>
 <body>
	<div id="header">替换文本中的关键字</div>
	<div id="content">
	<center>替换文本中的关键字</center>
	<center>
		<form method="post" action="./cyg.php">
			查询关键字:<input type="text" value="" name="old" size=12>
			&nbsp;<font color='red'>*</font><br>
			替换关键字:<input type="text" value="" name="new" size=12>
			&nbsp;<font color='red'>*</font><br>
			<em>提示:单击提交进行替换!</em>
			<input type="submit" value="提交" name='sel' id="input">
		</form>
	</center>
	<div id="showtime">
	<p>
	<?php
	$str = file_get_contents('./1.txt');//读取外部文件中的信息
	if(!empty($_POST['old'])){//判断是否为空提交
		$str_old = $_POST['old'];//获取查询的值
		$str_new = $_POST['new'];//把某字符替换成什么字符标记为红色
		$str = str_replace($str_old,$str_new,$str);//第一个参数要被替换的关键词.第二个参数:替换成什么。第三个参数:从哪一个变量里面把什么替换成什么
	}
	echo file_put_contents("1.txt",$str);
	?>
	</p>
	</div>
	</div>

 </body>
</html>

1.txt:

代码语言:javascript
复制
123456789

点击提交的内容是:

原先的1.txt文件里面的内容:

替换后的:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • cyg.php
  • 点击提交的内容是:
  • 原先的1.txt文件里面的内容:
  • 替换后的:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档