用于查找具有属性但顺序唯一的标签的正则表达式

我试图从 html 和 idk 中找到日期和交易,如何使用正则表达式获取 html。我尝试了简单的 html 我发现它更难实现。我想要做的是找到交易日期并将其放入数组中,然后以一种很好的格式打印出来。我试图从这个 html 的表格中获取某些值。日期的 html 是 (div style="width:100%;overflow:hidden;") 和 transctions 都是 (td align="right")。这是 html ( https://pastebin.com/L8emba2X )


我一直在弄乱许多不同版本的正则表达式,但由于只有一个日期,但每个日期要提取 4 个其他事务,因此它们似乎都不起作用。


$text='/<\s*div style="width:100%;overflow:hidden;"[^>]*>(.*?)<\/div[^>]>| 

<td align="right">(.+?)<\/td>/m';


preg_match_all($text, $html, $matches, PREG_SET_ORDER, 0);


print_r($matches);

我用过这个,只有交易出来了。还知道如何打印出某个交易的日期。


开心每一天1111
浏览 132回答 1
1回答

子衿沉夜

我的猜测是,在这里您可能需要类似于以下内容的表达式:<\s*div style="width:100%;overflow:hidden;"\s*>([\s\S]*?)<\/div>|<td align="right">(.+?)<\/td>用于捕获任何字符和换行符使用([\s\S]*?).您的尝试很好,但是,(.*?)不会通过换行符,这些将:([\s\S]*?)([\d\D]*?)([\w\W]*?)测试$re = '/<\s*div style="width:100%;overflow:hidden;"\s*>([\s\S]*?)<\/div>|<td align="right">(.+?)<\/td>/m';$str = '<td align="left"><div style="width:100%;overflow:hidden;">&nbsp; &nbsp; 2019.06.04 09:35</div></td><td>5176</td><td align="right">0.00</td><td align="right">0.00</td><td align="right">5,000.00</td><td align="right">5,000.00</td><td>орлого</td><td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td></tr><tr><td align="left"><div style="width:100%;overflow:hidden;">&nbsp; &nbsp; 2019.06.04 09:35</div></td><td>5024</td><td align="right">5,000.00</td><td align="right">-50.00</td><td align="right">0.00</td><td align="right">4,950.00</td><td>Ухаалаг мэдээ үйлчилгээний хураамж</td><td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td></tr><tr><td align="left"><div style="width:100%;overflow:hidden;">&nbsp; &nbsp; 2019.06.14 11:00&nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; </table></div>';preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);// Print the entire match resultvar_dump($matches);输出array(11) {&nbsp; [0]=>&nbsp; array(2) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(69) "<div style="width:100%;overflow:hidden;">&nbsp; &nbsp; 2019.06.04 09:35</div>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(22) "&nbsp; &nbsp; 2019.06.04 09:35"&nbsp; }&nbsp; [1]=>&nbsp; array(3) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(27) "<td align="right">0.00</td>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(0) ""&nbsp; &nbsp; [2]=>&nbsp; &nbsp; string(4) "0.00"&nbsp; }&nbsp; [2]=>&nbsp; array(3) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(27) "<td align="right">0.00</td>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(0) ""&nbsp; &nbsp; [2]=>&nbsp; &nbsp; string(4) "0.00"&nbsp; }&nbsp; [3]=>&nbsp; array(3) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(31) "<td align="right">5,000.00</td>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(0) ""&nbsp; &nbsp; [2]=>&nbsp; &nbsp; string(8) "5,000.00"&nbsp; }&nbsp; [4]=>&nbsp; array(3) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(31) "<td align="right">5,000.00</td>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(0) ""&nbsp; &nbsp; [2]=>&nbsp; &nbsp; string(8) "5,000.00"&nbsp; }&nbsp; [5]=>&nbsp; array(2) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(69) "<div style="width:100%;overflow:hidden;">&nbsp; &nbsp; 2019.06.04 09:35</div>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(22) "&nbsp; &nbsp; 2019.06.04 09:35"&nbsp; }&nbsp; [6]=>&nbsp; array(3) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(31) "<td align="right">5,000.00</td>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(0) ""&nbsp; &nbsp; [2]=>&nbsp; &nbsp; string(8) "5,000.00"&nbsp; }&nbsp; [7]=>&nbsp; array(3) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(29) "<td align="right">-50.00</td>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(0) ""&nbsp; &nbsp; [2]=>&nbsp; &nbsp; string(6) "-50.00"&nbsp; }&nbsp; [8]=>&nbsp; array(3) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(27) "<td align="right">0.00</td>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(0) ""&nbsp; &nbsp; [2]=>&nbsp; &nbsp; string(4) "0.00"&nbsp; }&nbsp; [9]=>&nbsp; array(3) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(31) "<td align="right">4,950.00</td>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(0) ""&nbsp; &nbsp; [2]=>&nbsp; &nbsp; string(8) "4,950.00"&nbsp; }&nbsp; [10]=>&nbsp; array(2) {&nbsp; &nbsp; [0]=>&nbsp; &nbsp; string(99) "<div style="width:100%;overflow:hidden;">&nbsp; &nbsp; 2019.06.14 11:00&nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; </table></div>"&nbsp; &nbsp; [1]=>&nbsp; &nbsp; string(52) "&nbsp; &nbsp; 2019.06.14 11:00&nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; </table>"&nbsp; }}分享
打开App,查看更多内容
随时随地看视频慕课网APP