@@ -33,6 +33,29 @@ public static function parseAttribute( $html, $selector, $attribute, $default =
33
33
return $ value ;
34
34
}
35
35
36
+ /**
37
+ * Parses the given HTML string to extract the specified attribute selector of the first node.
38
+ *
39
+ * @param string $html The HTML string to parse.
40
+ * @param string $attribute The attribute to extract of the first node.
41
+ *
42
+ * @return string|null extracted attribute
43
+ */
44
+ public static function parseFirstNodeAttribute ( $ html , $ attribute ) {
45
+ $ value = null ;
46
+ if ( trim ( empty ( $ html ) ) ) {
47
+ return $ value ;
48
+ }
49
+ $ doc = new Document ( $ html );
50
+ // <html><body>$html</body></html>
51
+ $ elem = $ doc ->find ( '* ' )[2 ];
52
+ if ( $ elem ) {
53
+ $ value = $ elem ->getAttribute ( $ attribute );
54
+ }
55
+
56
+ return $ value ;
57
+ }
58
+
36
59
/**
37
60
* Parses the given HTML string to extract the innerHTML contents.
38
61
*
@@ -53,16 +76,16 @@ public static function parseHTML( $html, $selector, $default = null ) {
53
76
}
54
77
return $ inner_html ;
55
78
}
56
-
79
+
57
80
/**
58
81
* Parses the given HTML string and extracts the specified elements.
59
- *
82
+ *
60
83
* @param string $html The HTML string to parse.
61
84
* @param string $element The element (selector) to extract.
62
- *
85
+ *
63
86
* @return string|null the HTML string of the extracted elements
64
87
*/
65
- public static function getElementsFromHTML ($ html , $ element ) {
88
+ public static function getElementsFromHTML ( $ html , $ element ) {
66
89
$ doc = new Document ();
67
90
$ doc ->loadHTML ( $ html );
68
91
$ elements = $ doc ->find ( $ element );
@@ -79,18 +102,18 @@ public static function getElementsFromHTML($html, $element) {
79
102
/**
80
103
* Gets the text content of a given selector. If multiple selectors exist,
81
104
* the first result will be used.
82
- *
105
+ *
83
106
* @param string $html The HTML string to parse.
84
107
* @param string $selector The selector to get the text content from.
85
- *
108
+ *
86
109
* @return string|null The text content of the selector if found.
87
110
*/
88
- public static function getTextFromSelector ($ html , $ selector ) {
111
+ public static function getTextFromSelector ( $ html , $ selector ) {
89
112
$ doc = new Document ();
90
113
$ doc ->loadHTML ( $ html );
91
114
$ nodes = $ doc ->find ( $ selector );
92
115
93
- if ( count ( $ nodes ) === 0 ) {
116
+ if ( count ( $ nodes ) === 0 ) {
94
117
return null ;
95
118
}
96
119
0 commit comments