<?php $str = 'windows NT windows 2003 windows xp'; preg_match_all('/windows ([^xp])/',$str,$res); print_r($res); ?>为什么运行的结果是这样的
时间: 2024-09-12 08:04:41 浏览: 48
这段PHP代码使用了正则表达式preg_match_all()函数来匹配字符串$str中符合模式"windows ([^xp])"的所有子串。该正则表达式的含义是查找以"windows"开头,后面跟着任意非"xp"字符的序列。
解释一下正则表达式:
- `/windows `:开始查找以"windows "(注意空格)开头的子串。
- `([^xp])`:这部分表示一个捕获组,其中[^xp]代表除了"x"和"p"之外的任何字符,并且每个字符都是一个单独的分组。
运行结果打印出的是所有匹配的部分,组成一个二维数组,因为每个匹配的子串都被单独捕获并作为子数组的成员。所以,如果字符串中有多个匹配,比如`"windows NT"`, `"windows 2003"`,结果数组将包含这些匹配的子串,对应于`$res[1]`。
例如,输出可能是类似这样的:
```json
Array
(
[0] => Array
(
[0] => windows NT
[1] => windows 2003
)
[1] => Array
(
[0] => NT
[1] => 2003
)
)
```
相关问题
【程序阅读题:】填空补充(为了自动阅卷,不要填写多余空格;如$a=2;不要写成$a = 2 ;或者$a =2 ;或者$a =2 ;等等) <form method="post"> ID :<input type="text" name="id"><br> 密码:<input type="password" name="pass"><br> <input type="submit" name="postmethod" value="登录"> </form> <?php if(isset($_POST["postmethod"])) //判定是否点击了登录按钮 { $userid = $_POST[“id”]; $flag = preg_match("/^[1-9]{3,6}$/", $userid); //$flag:检查$userid是否在3~6位数字之间:是1否0 $password= ____________________ ; //接收表单中填写的密码值 } ?>
<form method="post">
ID :<input type="text" name="id"><br>
密码:<input type="password" name="pass"><br>
<input type="submit" name="postmethod" value="登录">
</form>
<?php
if(isset($_POST["postmethod"])) //判定是否点击了登录按钮
{
$userid = $_POST["id"];
$flag = preg_match("/^[1-9]{3,6}$/", $userid); //$flag:检查$userid是否在3~6位数字之间:是1否0
$password= $_POST["pass"]; //接收表单中填写的密码值
}
?>
<?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`。
需要注意的是,这段代码仅用于演示上传过程,并没有涉及其他安全性措施,例如对上传文件的类型、大小、目录权限等进行验证和限制。在实际应用中,需要根据具体需求和安全考虑添加相应的验证和过滤机制。
阅读全文
相关推荐

















