@@ -37,7 +37,15 @@ public function remove(){
37
37
return $ this ;
38
38
}
39
39
40
- public function str ($ str ){ return new Str ($ str ); }
40
+ public function str (){ return new Str ($ this ->text ); }
41
+ public function match ($ re ){
42
+ $ str = new Str ($ this ->text );
43
+ return $ str ->match ($ re );
44
+ }
45
+ public function scan ($ re ){
46
+ $ str = new Str ($ this ->text );
47
+ return $ str ->scan ($ re );
48
+ }
41
49
public function clean ($ str ){ return trim (preg_replace ('/\s+/ ' , ' ' , $ str )); }
42
50
public function trim ($ str ){ return trim ($ str ); }
43
51
@@ -76,7 +84,7 @@ public function __call($key, $args){
76
84
case 'index ' : return $ this ->search ('./preceding-sibling::* ' )->length + 1 ;
77
85
78
86
/*
79
- DOMNode::insertBefore — Adds a new child
87
+ DOMNode::insertBefore — Adds a new child
80
88
*/
81
89
82
90
// simple-html-dom junk methods
@@ -97,10 +105,14 @@ public function __call($key, $args){
97
105
98
106
// attributes
99
107
case 'hasattribute ' : return !$ this ->is_text && $ this ->node ->getAttribute ($ args [0 ]);
100
- case 'getattribute ' : return $ this ->$ args [0 ];
108
+ case 'getattribute ' : $ arg = $ args [0 ]; return $ this ->$ arg ;
109
+ case 'setattribute ' : $ arg0 = $ args [0 ]; $ arg1 = $ args [1 ]; return $ this ->$ arg0 = $ arg1 ;
110
+ case 'removeattribute ' : $ arg = $ args [0 ]; return $ this ->$ arg = null ;
111
+ case 'getattribute ' : return $ this ->node ->getAttribute ($ args [0 ]);
101
112
case 'setattribute ' : return $ this ->$ args [0 ] = $ args [1 ];
102
113
case 'removeattribute ' : return $ this ->$ args [0 ] = null ;
103
114
115
+
104
116
// wrap
105
117
case 'wrap ' :
106
118
return $ this ->replace ('< ' . $ args [0 ] . '> ' . $ this . '</ ' . $ args [0 ] . '> ' );
@@ -129,9 +141,11 @@ public function __call($key, $args){
129
141
// $doc->spans[x]
130
142
if (preg_match (TAGS_REGEX , $ key , $ m )) return $ this ->find ($ m [1 ]);
131
143
if (preg_match (TAG_REGEX , $ key , $ m )) return $ this ->find ($ m [1 ], 0 );
132
-
133
- if (preg_match ('/(clean|trim|str)(.*)/ ' , $ key , $ m )){
134
- return $ this ->$ m [1 ]($ this ->$ m [2 ]);
144
+
145
+ if (preg_match ('/(clean|trim|str)(.*)/ ' , $ key , $ m ) && isset ($ m [2 ])){
146
+ $ arg1 = $ m [1 ];
147
+ $ arg2 = $ m [2 ];
148
+ return $ this ->$ arg1 ($ this ->$ arg2 );
135
149
}
136
150
137
151
if (!preg_match (ATTRIBUTE_REGEX , $ key , $ m )) trigger_error ('Unknown method or property: ' . $ key , E_USER_WARNING );
@@ -159,7 +173,7 @@ public function load($html, $is_xml = false){
159
173
@$ this ->dom ->loadXML (preg_replace ('/xmlns=".*?"/ ' , '' , $ html ));
160
174
} else {
161
175
@$ this ->dom ->loadHTML ($ html );
162
- }
176
+ }
163
177
$ this ->xpath = new DOMXPath ($ this ->dom );
164
178
//$this->root = new AHTMLNode($this->dom->documentElement, $this->doc);
165
179
$ this ->root = $ this ->at ('body ' );
@@ -189,7 +203,7 @@ function __construct($nodeList, $doc){
189
203
abstract public void offsetUnset ( mixed $offset )
190
204
*/
191
205
192
- public function offsetExists ($ offset ){ return 0 <= $ offset && $ offset < $ this ->nodeList ->length () ; }
206
+ public function offsetExists ($ offset ){ return 0 <= $ offset && $ offset < $ this ->nodeList ->length ; }
193
207
public function offsetGet ($ offset ){ return new AHTMLNode ($ this ->nodeList ->item ($ offset ), $ this ->doc ); }
194
208
public function offsetSet ($ offset , $ value ){ trigger_error ('offsetSet not implemented ' , E_USER_WARNING ); }
195
209
public function offsetUnset ($ offset ){ trigger_error ('offsetUnset not implemented ' , E_USER_WARNING ); }
@@ -201,23 +215,23 @@ public function count(){
201
215
public function rewind (){
202
216
$ this ->counter = 0 ;
203
217
}
204
-
218
+
205
219
public function current (){
206
220
return new AHTMLNode ($ this ->nodeList ->item ($ this ->counter ), $ this ->doc );
207
221
}
208
-
222
+
209
223
public function key (){
210
224
return $ this ->counter ;
211
225
}
212
-
226
+
213
227
public function next (){
214
228
$ this ->counter ++;
215
229
}
216
-
230
+
217
231
public function valid (){
218
232
return $ this ->counter < $ this ->nodeList ->length ;
219
233
}
220
-
234
+
221
235
public function last (){
222
236
return ($ this ->nodeList ->length > 0 ) ? new AHTMLNode ($ this ->nodeList ->item ($ this ->nodeList ->length - 1 ), $ this ->doc ) : null ;
223
237
}
@@ -279,12 +293,18 @@ public function __call($key, $values){
279
293
*/
280
294
281
295
if (preg_match (ATTRIBUTES_REGEX , $ key , $ m ) || preg_match ('/^((clean|trim|str).*)s$/ ' , $ key , $ m )){
282
- foreach ($ this as $ node ){$ retval [] = $ node ->$ m [1 ];}
296
+ foreach ($ this as $ node ){
297
+ $ arg = $ m [1 ];
298
+ $ retval [] = $ node ->$ arg ;
299
+ }
283
300
return $ retval ;
284
301
}
285
302
286
303
if (preg_match (ATTRIBUTE_REGEX , $ key , $ m )){
287
- foreach ($ this as $ node ){$ retval [] = $ node ->$ m [1 ];}
304
+ foreach ($ this as $ node ){
305
+ $ arg = $ m [1 ];
306
+ $ retval [] = $ node ->$ arg ;
307
+ }
288
308
return implode ('' , $ retval );
289
309
}
290
310
@@ -304,7 +324,7 @@ public function length(){ return $this->nodeList->length; }
304
324
305
325
class AHTMLNode extends AdvancedHtmlBase implements ArrayAccess{
306
326
private $ _path ;
307
-
327
+
308
328
function __construct ($ node , $ doc ){
309
329
$ this ->node = $ node ;
310
330
$ this ->_path = $ node ->getNodePath ();
@@ -321,13 +341,13 @@ private function get_fragment($html){
321
341
322
342
function replace ($ html ){
323
343
$ node = empty ($ html ) ? null : $ this ->before ($ html );
324
- $ this ->remove ();
344
+ $ this ->remove ();
325
345
return $ node ;
326
346
}
327
347
328
348
function before ($ html ){
329
349
$ fragment = $ this ->get_fragment ($ html );
330
- $ this ->node ->parentNode ->insertBefore ($ fragment , $ this ->node );
350
+ $ this ->node ->parentNode ->insertBefore ($ fragment , $ this ->node );
331
351
return new AHTMLNode ($ this ->node ->previousSibling , $ this ->doc );
332
352
}
333
353
@@ -341,7 +361,7 @@ function after($html){
341
361
}
342
362
343
363
function decamelize ($ str ){
344
- $ str = preg_replace ('/(^|[a-z])([A-Z])/e ' , 'strtolower(strlen(" \\1") ? " \\1_ \\2" : " \\2") ' , $ str );
364
+ $ str = preg_replace ('/(^|[a-z])([A-Z])/e ' , 'strtolower(strlen(" \\1") ? " \\1_ \\2" : " \\2") ' , $ str );
345
365
return preg_replace ('/ / ' , '_ ' , strtolower ($ str ));
346
366
}
347
367
@@ -409,7 +429,7 @@ public function offsetSet($key, $value){
409
429
} else {
410
430
$ this ->node ->removeAttribute ($ key );
411
431
}
412
- //trigger_error('offsetSet not implemented', E_USER_WARNING);
432
+ //trigger_error('offsetSet not implemented', E_USER_WARNING);
413
433
}
414
434
public function offsetUnset ($ offset ){ trigger_error ('offsetUnset not implemented ' , E_USER_WARNING ); }
415
435
@@ -490,6 +510,8 @@ private static function not($str){
490
510
switch (true ){
491
511
case preg_match ('/^\.(\w+)$/ ' , $ str , $ m ): return self ::do_class ($ str );
492
512
case preg_match ('/^\#(\w+)$/ ' , $ str , $ m ): return self ::do_id ($ str );
513
+ case preg_match ('/^(\w+)$/ ' , $ str , $ m ): return "self:: " . $ str ;
514
+ case preg_match ('/^\[(.*)\]$/ ' , $ str , $ m ): return substr (self ::do_braces ($ str ), 1 , -1 );
493
515
default : return self ::translate ($ str );
494
516
}
495
517
}
@@ -551,7 +573,7 @@ static function do_braces($str){
551
573
$ tokens = preg_split ($ re , substr ($ str , 1 , strlen ($ str ) - 2 ), 0 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
552
574
// var_dump($tokens);
553
575
$ attr = trim (array_shift ($ tokens ));
554
- // && )
576
+ // && )
555
577
if (!$ op = @trim (array_shift ($ tokens ))){
556
578
switch (true ){
557
579
case preg_match ('/^\d+$/ ' , $ attr ): return "[count(preceding-sibling::*) = " . ($ attr - 1 ) . "] " ; // [2] -> [count(preceding-sibling::*) = 1]
@@ -610,7 +632,7 @@ static function translate($str){
610
632
$ retval = array ();
611
633
$ re = '/(\((?>[^()]|(?R))*\)|\[(?>[^\[\]]|(?R))*\]|\s*[+~>]\s*| \s*)/ ' ;
612
634
$ item = '' ;
613
-
635
+
614
636
$ last_nav = null ;
615
637
//echo "\n!" . $str . "!\n";
616
638
//var_dump(preg_split($re, $str, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
@@ -621,7 +643,7 @@ static function translate($str){
621
643
case '> ' :
622
644
case '~ ' :
623
645
case '+ ' :
624
- case '' :
646
+ case '' :
625
647
if (!empty ($ item )) $ retval [] = self ::translate_part (trim ($ item ), $ last_nav );
626
648
$ item = '' ;
627
649
$ last_nav = $ token ;
@@ -682,7 +704,7 @@ public static function xpath_for($str){
682
704
* Str
683
705
*/
684
706
685
- class Str{
707
+ class Str{
686
708
var $ text ;
687
709
688
710
function __construct ($ str ){
@@ -692,7 +714,7 @@ function __construct($str){
692
714
function match ($ regex , $ group_number = 0 ){
693
715
if (!preg_match ($ regex , $ this ->text , $ m )) return false ;
694
716
$ val = $ m [$ group_number ];
695
- return new Str ($ val );
717
+ return $ val ; // new Str($val);
696
718
}
697
719
698
720
function scan ($ regex , $ group_number = 0 ){
@@ -728,4 +750,5 @@ function file_get_html($url){ return str_get_html(file_get_contents($url)); }
728
750
function str_get_xml ($ html ){ return new AdvancedHtmlDom ($ html , true ); }
729
751
function file_get_xml ($ url ){ return str_get_xml (file_get_contents ($ url )); }
730
752
}
731
- ?>
753
+
754
+ ?>
0 commit comments