preg_replace('/(' . $pattern . ')/ei','print_r("\\1")', $code);
时间: 2024-02-07 08:03:55 浏览: 17
preg_replace('/(' . $pattern . ')/e','print_r("\\1")', $code)是一个用于替换字符串中匹配正则表达式模式的函数。该函数的作用是将匹配的模式替换为指定的内容,其中$pattern是要匹配的模式,$code是要进行替换的字符串。
在这个例子中,使用eval模式修饰符'e'来执行替换操作。在替换过程中,匹配的模式会被传递给print_r("\\1")函数进行打印输出。
相关问题
<?php $allow_ext = array('png','jpg','gif'); $file = $_FILES['file']['name']; $file_info = pathinfo($file); var_dump($file_info); $file_ext = $file_info['extension']; if(!in_array($file_ext, $allow_ext)){ exit("error extension"); } $news = time(); $filepath = './uploads/'; $newname = md5(md5($news)).'.'.$file_ext; $tmp_name=$_FILES['file']['tmp_name']; $filename=$filepath.$newname; if(move_uploaded_file($tmp_name,$filename)) { $content = file_get_contents($filename); $content = preg_replace('/<\?php|\?>/i', '_', $content); $content = preg_replace('/php/i', '', $content); $content = preg_replace('/eval/i', '_', $content); $content = preg_replace('/assert/i', '_', $content); $content = preg_replace('/ev/i', '_', $content); $content = preg_replace('/system/i', '_', $content); $content = preg_replace('/as/i', '_', $content); $content = preg_replace('/call/i', '_', $content); $content = preg_replace('/replace/i', '_', $content); $content = preg_replace('/func/i', '_', $content); file_put_contents($filename, $content); $data['success'] = 1; $data['message'] = 'æˆåŠŸ'; $data['url'] = $filename; }else{ $data['message']='ä¸Šä¼ å¤±è´¥!'; } echo json_encode($data); ?>
这段代码是一个 PHP 文件上传的示例。它首先定义了允许上传的文件扩展名数组 `$allow_ext`,然后通过 `$_FILES` 数组获取上传文件的原始名称,并使用 `pathinfo()` 函数获取文件信息。接下来,它将获取到的文件扩展名与允许的扩展名进行比较,如果不在允许的范围内,则输出 "error extension" 并终止程序。
如果文件扩展名允许上传,则会生成一个新的文件名 `$newname`,这里使用了时间戳和 MD5 哈希来生成唯一的新文件名,并将其保存在指定的文件夹路径 `$filepath` 中。
然后,它使用 `move_uploaded_file()` 函数将临时文件移动到指定的路径中。接下来,它会读取上传文件的内容,并使用一系列正则表达式替换操作来过滤一些敏感关键词(如 PHP 标签、eval、assert 等),并将过滤后的内容重新写入文件。
最后,它会将上传结果以 JSON 格式输出,包含成功标识 `success`、消息 `message` 和上传文件的 URL `url`。
需要注意的是,这段代码仅用于演示上传过程,并没有涉及其他安全性措施,例如对上传文件的类型、大小、目录权限等进行验证和限制。在实际应用中,需要根据具体需求和安全考虑添加相应的验证和过滤机制。
preg_match($pattern,$str,$arr)
这是 PHP 语言中用于正则表达式匹配的函数,具体参数含义如下:
- $pattern:要匹配的正则表达式
- $str:要检索的字符串
- $arr:匹配结果保存到该数组中
该函数会在 $str 中查找符合 $pattern 的字符串,并将匹配结果保存到 $arr 数组中。如果匹配成功,函数返回 1;否则返回 0。