-
Notifications
You must be signed in to change notification settings - Fork 20
Query Filter
Thomas Weinert edited this page Jul 30, 2014
·
1 revision
FluentDOM\Query filter(string|callable $selector);
Removes all elements from the set of matched elements that do not match the specified expression.
$html = <<<HTML
<html>
<head>
<title>Examples: FluentDOM\Query::filter() with expression</title>
</head>
<body>
<div/>
<div class="middle"/>
<div class="middle"/>
<div class="middle"/>
<div class="middle"/>
<div/>
</body>
</html>
HTML;
echo FluentDOM($html, 'text/html')
->find('//div')
->attr('border', 1)
->filter('@class = "middle"')
->attr('style', 'text-align: center;');
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><title>Examples: FluentDOM\Query::filter() with expression</title></head><body>
<div border="1"></div>
<div class="middle" border="1" style="text-align: center;"></div>
<div class="middle" border="1" style="text-align: center;"></div>
<div class="middle" border="1" style="text-align: center;"></div>
<div class="middle" border="1" style="text-align: center;"></div>
<div border="1"></div>
</body></html>
$html = <<<HTML
<html>
<head>
<title>Examples: FluentDOM\Query::filter() with callback function</title>
</head>
<body>
<div id="first"> </div>
<div id="second"> </div>
<div id="third"> </div>
<div id="fourth"> </div>
<div id="fifth"> </div>
<div id="sixth"> </div>
</body>
</html>
HTML;
echo FluentDOM($html)
->find('//div')
->attr('border', 1)
->filter(
function($node, $index) {
if ($index == 1 ||
FluentDOM($node)->attr('id') == 'fourth') {
return TRUE;
}
return FALSE;
}
)
->attr('style', 'text-align: center;');
<?xml version="1.0"?>
<html>
<head>
<title>Examples: FluentDOM\Query::filter() with callback function</title>
</head>
<body>
<div id="first" border="1"> </div>
<div id="second" border="1" style="text-align: center;"> </div>
<div id="third" border="1"> </div>
<div id="fourth" border="1" style="text-align: center;"> </div>
<div id="fifth" border="1"> </div>
<div id="sixth" border="1"> </div>
</body>
</html>
- Home
- Getting Started
- Tasks
- Plugins
- Functions
- Lists
- Creator (5.1)
- CSS Selectors
- Convertors
- Loaders
- Serializers (5.1)
- Transformers (5.1)
- Extended DOM
- XMLReader (6.1)
- XMLWriter (6.1)
- Interfaces