php find all elements with style attribute using preg_match_all
I just want to find ALL elements with preg_match_all in a html document. After reading the file i am using the following:
preg_match_all('<.*style=?.*>',$file,$patterns);
print_r( $patterns[0] ); die;
Gives all the elements but with spacing and other stuff before the < and >. Also the output has an end tag in the result (for example: '). I have play around with preg expressions but drives me insane. Can somebody tell me what is the correct syntax to use?
The output is now:
Array
(
[0] => <table style="position:absolute;width:100%;height:100%;">
[1] => <div class="_barcode_pdf417" style="margin:0 auto;width:176px;height:132px;background:#FFF;color:#000;"><div style="margin:0 auto;margin:0;padding:0;border:0">
[2] => <div style="position:absolute;width:14px;height:128px;background:#000;"></div>
[3] => <div style="position:absolute;margin-left:18px;width:2px;height:128px;background:#000;"></div>
[4] => <div style="position:absolute;margin-left:22px;width:2px;height:128px;background:#000;"></div>
[5] => <div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;"></div>
........
........
........
But i want:
Array
(
[0] => <table style="position:absolute;width:100%;height:100%;">
[1] => <div class="_barcode_pdf417" style="margin:0 auto;width:176px;height:132px;background:#FFF;color:#000;">
&开发者_JS百科lt;div style="margin:0 auto;margin:0;padding:0;border:0">
[2] => <div style="position:absolute;width:14px;height:128px;background:#000;">
[3] => <div style="position:absolute;margin-left:18px;width:2px;height:128px;background:#000;">
[4] => <div style="position:absolute;margin-left:22px;width:2px;height:128px;background:#000;">
[5] => <div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;">
......
......
Thank you for your answer! Kind regards.
$html = <<< EOF
[0] => <table style="position:absolute;width:100%;height:100%;">
[1] => <div class="_barcode_pdf417" style="margin:0 auto;width:176px;height:132px;background:#FFF;color:#000;"><div style="margin:0 auto;margin:0;padding:0;border:0">
[2] => <div style="position:absolute;width:14px;height:128px;background:#000;"></div>
[3] => <div style="position:absolute;margin-left:18px;width:2px;height:128px;background:#000;"></div>
[4] => <div style="position:absolute;margin-left:22px;width:2px;height:128px;background:#000;"></div>
[5] => <div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;"></div>
........
........
........"
EOF;
preg_match_all('/([<div|<table]+.*?style.*?>)/i', $html, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
echo $result[1][$i];
}
will output:
<table style="position:absolute;width:100%;height:100%;">
<div class="_barcode_pdf417" style="margin:0 auto;width:176px;height:132px;background:#FFF;color:#000;">
<div style="margin:0 auto;margin:0;padding:0;border:0">
<div style="position:absolute;width:14px;height:128px;background:#000;">
<div style="position:absolute;margin-left:18px;width:2px;height:128px;background:#000;">
<div style="position:absolute;margin-left:22px;width:2px;height:128px;background:#000;">
<div style="position:absolute;margin-left:26px;width:2px;height:128px;background:#000;">
although, the best option is to use an html dom parser
I would strongly advise against using regex for operating on (X)HTML in this fashion, as PHP provides a higher level API for the job in the form of the DOMDocument extension. You can use it to iterate over a valid DOm structure and find elements with specific attributes. Operation is fairly similar to Javascript DOM manipulation with features such as GetElementById, GetElementByClassName and suchlike available to you.
You could use it to iterate of the body's child elements (and their children recursively) to find the elements with styles defined.
精彩评论