[英]php - get all the values for a specific attribute for all input tags on a website
My goal is to get all the values of the "name" attribute in every <input>
tag from the html source that is given on html.txt. 我的目标是从html.txt上提供的html源中的每个<input>
标记中获取“ name”属性的所有值。 I need help with the regex and also with the if statement, because right now it's only outputting the first value that is found using the preg_match and not the rest (and it's not even checking if the name attribute is in an input tag). 我需要有关正则表达式以及if语句的帮助,因为现在它仅输出使用preg_match找到的第一个值,而不输出其余的值(它甚至不检查name属性是否在输入标记中)。 Also I'm trying to have the regex fixed as well so it doesn't actually show the name= part. 另外,我也在尝试修复正则表达式,因此它实际上并未显示name =部分。
<?php
$myfile = fopen('html.txt', 'r');
$subject = fread($myfile,filesize("html.txt"));
$pattern='/name="([^"]*)\"/';
if(preg_match($pattern, $subject, $hits)){
echo "values: $hits[0]";
}
?>
example output: 示例输出:
values: name="title"
Rather than using regex to find the attributes and values, why not parse the html dom. 与其使用正则表达式来查找属性和值,不如解析html dom。 Use simpleXML or http://php.net/manual/en/domdocument.loadhtml.php to create an array based on the HTML DOM of the document, then run through the array to locate the input elements and their attributes. 使用simpleXML或http://php.net/manual/zh/domdocument.loadhtml.php基于文档的HTML DOM创建一个数组,然后遍历该数组以找到输入元素及其属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.