php 使用 preg_match_all 查找所有具有 style 属性的元素
我只想在 html 文档中找到带有 preg_match_all 的所有元素。阅读文件后,我使用以下内容:
preg_match_all('<.*style=?.*>',$file,$patterns);
print_r( $patterns[0] ); die;
给出所有元素,但在 << 之前有间距和其他内容。和>。此外,输出结果中还有一个结束标记(例如:')。我玩过 preg 表达式,但让我发疯。有人可以告诉我使用正确的语法是什么吗?
现在的输出是:
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>
........
........
........
但我想要:
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;">
[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;">
......
......
谢谢您的回答!亲切的问候。
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;">
<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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将输出:
虽然,最好的选择是使用 html dom 解析器
will output:
although, the best option is to use an html dom parser
我强烈建议不要使用正则表达式以这种方式在 (X)HTML 上进行操作,因为 PHP 以 DOMDocument 扩展的形式为该作业提供了更高级别的 API。您可以使用它来迭代有效的 DOm 结构并查找具有特定属性的元素。操作与 Javascript DOM 操作非常相似,具有 GetElementById、GetElementByClassName 等功能可供您使用。
您可以使用它来迭代主体的子元素(及其递归子元素)以查找已定义样式的元素。
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.