简体   繁体   中英

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. 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). Also I'm trying to have the regex fixed as well so it doesn't actually show the name= part.

<?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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM