![](/img/trans.png)
[英]php get_tags() to list all the tags for the products in a specific category in woocommerce
[英]php - get all the values for a specific attribute for all input tags on a website
我的目標是從html.txt上提供的html源中的每個<input>
標記中獲取“ name”屬性的所有值。 我需要有關正則表達式以及if語句的幫助,因為現在它僅輸出使用preg_match找到的第一個值,而不輸出其余的值(它甚至不檢查name屬性是否在輸入標記中)。 另外,我也在嘗試修復正則表達式,因此它實際上並未顯示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]";
}
?>
示例輸出:
values: name="title"
與其使用正則表達式來查找屬性和值,不如解析html dom。 使用simpleXML或http://php.net/manual/zh/domdocument.loadhtml.php基於文檔的HTML DOM創建一個數組,然后遍歷該數組以找到輸入元素及其屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.