可以使用以下正则表达式来匹配页面中 input 的 value 为 pdf 文件的情况:

/<input[^>]*value="(.*?)\.pdf"/i

解释:

  • <input[^>]*:匹配 input 标签,[^>]* 表示匹配除了 > 之外的任意字符,* 表示匹配 0 次或多次。
  • value=":匹配 value 属性的值。
  • (.*?):匹配任意字符,? 表示非贪婪模式,即匹配最短的字符串。
  • \.pdf":匹配以 .pdf 结尾的字符串。

使用 preg_match_all() 函数可以返回所有匹配的结果:

<?php
$str = '<input type="hidden" name="file" value="example.pdf">
<input type="text" name="name" value="John">
<input type="file" name="file" value="example2.pdf">';

preg_match_all('/<input[^>]*value="(.*?)\.pdf"/i', $str, $matches);

print_r($matches[1]); // 输出 Array ( [0] => example [1] => example2 )
?>

以上代码会输出所有匹配到的 pdf 文件名(不包括扩展名)。

php 正则匹配页面中 input里value为pdf文件

原文地址: https://www.cveoy.top/t/topic/zc4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录