簡體   English   中英

從文本和html標簽中檢測並提取圖像URL

[英]Detect and extract image url from text and html tags

如何檢測文本中是否存在某些圖像html標記並僅提取圖像的網址?

例如。

提取此網址:

http://
www.someurl.com/somefileprocessor.php/12345/somedir/somesubdir/someniceimage.j
pg

來自此標記(此標記可以在另一堆文本和/或html中)

<img title="Some nice title" border="0"
hspace="0" alt="some useful hint" src="http://
www.someurl.com/somefileprocessor.php/12345/somedir/somesubdir/someniceimage.j
pg" width="629" height="464" />

感謝提前Ângelo

快速嘗試<img/>標記特定的正則表達式:

preg_match_all('/<img[^>]*?\s+src\s*=\s*"([^"]+)"[^>]*?>/i', $str, $matches);

您可以使用CRUL獲取內容,然后從內容中提取所有img標記。 通過curl獲取數據:

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

然后使用正則表達式提取數據。

^https?://(?:[a-z\-]+\.)+[a-z]{2,6}(?:/[^/#?]+)+\.(?:jpg|gif|png)$

這有助於您提取所有圖像網址(在img標簽中或不是)。

如果您需要抓取工具,可以使用PHPCrawl

感謝awnswers,因為我學習了更多的PHP。 我嘗試這種快速而骯臟的方式,它也提取圖像網址

$imageurl    = strstr($title, 'src',FALSE);
$imageurl    = strstr($imageurl,'"',FALSE);
$imageurlpos = strpos($imageurl,'"');
$imageurl    = substr($imageurl,$imageurlpos+1);
$imageurlpos = strpos($imageurl,'"');
$imageurl    = substr($imageurl,0,$imageurlpos);

使用多個<a>標簽后代</a>抓取 HTML 頁面<div id="text_translate"><p>我在數據庫字段中有這個 html 源代碼。 我想分析這段代碼,特別是一些表格的字段,並將它們打印在屏幕上。 這是關於表的代碼:</p><pre> &lt;table cellspacing="1" cellpadding="1" class="troop_details inReturn" &gt; &lt;thead&gt; &lt;tr&gt; &lt;td class="role"&gt; &lt;a href="/karte.php?d=91628"&gt;01] #WorkInProgress&lt;/a&gt; &lt;/td&gt; &lt;td colspan="11" class="troopHeadline"&gt; &lt;a href="/karte.php?d=91611"&gt;Return from 01-soldier&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody class="units"&gt; &lt;tr&gt; &lt;th class="coords"&gt; &amp;#x202d;&lt;span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"&gt;&lt;span class="coordinateX"&gt;(&amp;#x202d;&amp;minus;&amp;#x202d;1&amp;#x202c;&amp;#x202c;&lt;/span&gt;&lt;span class="coordinatePipe"&gt;|&lt;/span&gt;&lt;span class="coordinateY"&gt;&amp;#x202d;&amp;minus;&amp;#x202d;28&amp;#x202c;&amp;#x202c;)&lt;/span&gt;&lt;/span&gt;&amp;#x202c; &lt;/th&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u21" title="Phalanx: 1:12:51" alt="Phalanx" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u22" title="Swordsman: 1:25:00" alt="Swordsman" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u23" title="Pathfinder: 0:30:00" alt="Pathfinder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u24" title="Theutates Thunder: 0:26:51" alt="Theutates Thunder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u25" title="Druidrider: 0:31:53" alt="Druidrider" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u26" title="Haeduan: 0:39:14" alt="Haeduan" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u27" title="Ram: 2:07:30" alt="Ram" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u28" title="Trebuchet: 2:50:00" alt="Trebuchet" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u29" title="Chieftain: 1:42:00" alt="Chieftain" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u30" title="Settler: 1:42:00" alt="Settler" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon last"&gt; &lt;img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="units last"&gt; &lt;tr&gt; &lt;th&gt;Troops&lt;/th&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit"&gt; 500 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Bounty&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="res"&gt; &lt;div class="inlineIconList resourceWrapper"&gt;&lt;div class="inlineIcon resources" title="Lumber"&gt;&lt;i class="r1"&gt;&lt;/i&gt;&lt;span class="value "&gt;6758&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Clay"&gt;&lt;i class="r2"&gt;&lt;/i&gt;&lt;span class="value "&gt;8093&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Iron"&gt;&lt;i class="r3"&gt;&lt;/i&gt;&lt;span class="value "&gt;6908&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Crop"&gt;&lt;i class="r4"&gt;&lt;/i&gt;&lt;span class="value "&gt;15741&lt;/span&gt;&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="carry"&gt; &lt;img class="carry full" title="carry" alt="carry" src="/img/x.gif"/&gt; &amp;#x202d;&amp;#x202d;37500&amp;#x202c;&amp;nbsp;/&amp;nbsp;&amp;#x202d;37500&amp;#x202c;&amp;#x202c; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Arrival&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="in"&gt;in&amp;nbsp;&lt;span class="timer" counting="down" value="85"&gt;0:01:25&lt;/span&gt;&amp;nbsp;hrs.&lt;/div&gt; &lt;div class="at"&gt;&lt;span&gt;at&amp;nbsp;00:43:10&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;a name="at"&gt;&lt;/a&gt; &lt;table cellspacing="1" cellpadding="1" class="troop_details inReturn" &gt; &lt;thead&gt; &lt;tr&gt; &lt;td class="role"&gt; &lt;a href="/karte.php?d=91628"&gt;01] #WorkInProgress&lt;/a&gt; &lt;/td&gt; &lt;td colspan="11" class="troopHeadline"&gt; &lt;a href="/karte.php?d=94829"&gt;Return from 0-New Hulk&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody class="units"&gt; &lt;tr&gt; &lt;th class="coords"&gt; &amp;#x202d;&lt;span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"&gt;&lt;span class="coordinateX"&gt;(&amp;#x202d;&amp;minus;&amp;#x202d;1&amp;#x202c;&amp;#x202c;&lt;/span&gt;&lt;span class="coordinatePipe"&gt;|&lt;/span&gt;&lt;span class="coordinateY"&gt;&amp;#x202d;&amp;minus;&amp;#x202d;28&amp;#x202c;&amp;#x202c;)&lt;/span&gt;&lt;/span&gt;&amp;#x202c; &lt;/th&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u21" title="Phalanx: 0:45:33" alt="Phalanx" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u22" title="Swordsman: 0:53:09" alt="Swordsman" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u23" title="Pathfinder: 0:18:46" alt="Pathfinder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u24" title="Theutates Thunder: 0:16:47" alt="Theutates Thunder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u25" title="Druidrider: 0:19:56" alt="Druidrider" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u26" title="Haeduan: 0:24:32" alt="Haeduan" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u27" title="Ram: 1:19:44" alt="Ram" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u28" title="Trebuchet: 1:46:18" alt="Trebuchet" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u29" title="Chieftain: 1:03:47" alt="Chieftain" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u30" title="Settler: 1:03:47" alt="Settler" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon last"&gt; &lt;img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="units last"&gt; &lt;tr&gt; &lt;th&gt;Troops&lt;/th&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit"&gt; 400 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Bounty&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="res"&gt; &lt;div class="inlineIconList resourceWrapper"&gt;&lt;div class="inlineIcon resources" title="Lumber"&gt;&lt;i class="r1"&gt;&lt;/i&gt;&lt;span class="value "&gt;6130&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Clay"&gt;&lt;i class="r2"&gt;&lt;/i&gt;&lt;span class="value "&gt;5835&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Iron"&gt;&lt;i class="r3"&gt;&lt;/i&gt;&lt;span class="value "&gt;5638&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Crop"&gt;&lt;i class="r4"&gt;&lt;/i&gt;&lt;span class="value "&gt;12397&lt;/span&gt;&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="carry"&gt; &lt;img class="carry full" title="carry" alt="carry" src="/img/x.gif"/&gt; &amp;#x202d;&amp;#x202d;30000&amp;#x202c;&amp;nbsp;/&amp;nbsp;&amp;#x202d;30000&amp;#x202c;&amp;#x202c; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Arrival&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="in"&gt;in&amp;nbsp;&lt;span class="timer" counting="down" value="920"&gt;0:15:20&lt;/span&gt;&amp;nbsp;hrs.&lt;/div&gt; &lt;div class="at"&gt;&lt;span&gt;at&amp;nbsp;00:57:05&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;</pre><p> 我感興趣的數據如下:</p><ol><li> 從 01-士兵<strong>歸來 00:43:10</strong></li><li> 從 0-新綠巨人<strong>歸來 00:57:05</strong></li></ol><p> 感謝您的建議,這是我目前的代碼:</p><pre> &lt;?php include 'database.php'?&gt; &lt;?php session_start(); ?&gt; &lt;?php include_once('simple_html_dom.php'); $caserma = $_SESSION["caserma"]; $dom = new DOMDocument; libxml_use_internal_errors(true); $dom-&gt;loadHTML($_SESSION["caserma"], LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $texts = []; foreach ($xpath-&gt;query("//table[contains(@class, 'troop_details') and contains(@class, 'inReturn')]//td[@class='troopHeadline']//a[@href]/text()") as $textNode) { $texts[] = $textNode-&gt;nodeValue; } var_export($texts); ?&gt;</pre><p> 但是作為 output 它給了我數組()</p></div>標簽並從特定的文本中提取文本<table> </table>

[英]Scrape HTML page with multiple <table> tags and extract text from specific <a> tag descendants

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 從rss feed中的html標簽提取文本 如何檢測文本中的HTML標簽? 從html字符串中檢測節點並在PHP中提取圖像 PHP-從網址中提取標簽 檢測圖像URL並包裝IMG標簽? PHP-如果僅包含HTML標記或帶有HTML標記的文本,則檢測字符串 從HTML提取文本 使用DOM從網址中提取HTML 使用多個<a>標簽后代</a>抓取 HTML 頁面<div id="text_translate"><p>我在數據庫字段中有這個 html 源代碼。 我想分析這段代碼,特別是一些表格的字段,並將它們打印在屏幕上。 這是關於表的代碼:</p><pre> &lt;table cellspacing="1" cellpadding="1" class="troop_details inReturn" &gt; &lt;thead&gt; &lt;tr&gt; &lt;td class="role"&gt; &lt;a href="/karte.php?d=91628"&gt;01] #WorkInProgress&lt;/a&gt; &lt;/td&gt; &lt;td colspan="11" class="troopHeadline"&gt; &lt;a href="/karte.php?d=91611"&gt;Return from 01-soldier&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody class="units"&gt; &lt;tr&gt; &lt;th class="coords"&gt; &amp;#x202d;&lt;span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"&gt;&lt;span class="coordinateX"&gt;(&amp;#x202d;&amp;minus;&amp;#x202d;1&amp;#x202c;&amp;#x202c;&lt;/span&gt;&lt;span class="coordinatePipe"&gt;|&lt;/span&gt;&lt;span class="coordinateY"&gt;&amp;#x202d;&amp;minus;&amp;#x202d;28&amp;#x202c;&amp;#x202c;)&lt;/span&gt;&lt;/span&gt;&amp;#x202c; &lt;/th&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u21" title="Phalanx: 1:12:51" alt="Phalanx" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u22" title="Swordsman: 1:25:00" alt="Swordsman" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u23" title="Pathfinder: 0:30:00" alt="Pathfinder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u24" title="Theutates Thunder: 0:26:51" alt="Theutates Thunder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u25" title="Druidrider: 0:31:53" alt="Druidrider" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u26" title="Haeduan: 0:39:14" alt="Haeduan" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u27" title="Ram: 2:07:30" alt="Ram" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u28" title="Trebuchet: 2:50:00" alt="Trebuchet" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u29" title="Chieftain: 1:42:00" alt="Chieftain" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u30" title="Settler: 1:42:00" alt="Settler" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon last"&gt; &lt;img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="units last"&gt; &lt;tr&gt; &lt;th&gt;Troops&lt;/th&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit"&gt; 500 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Bounty&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="res"&gt; &lt;div class="inlineIconList resourceWrapper"&gt;&lt;div class="inlineIcon resources" title="Lumber"&gt;&lt;i class="r1"&gt;&lt;/i&gt;&lt;span class="value "&gt;6758&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Clay"&gt;&lt;i class="r2"&gt;&lt;/i&gt;&lt;span class="value "&gt;8093&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Iron"&gt;&lt;i class="r3"&gt;&lt;/i&gt;&lt;span class="value "&gt;6908&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Crop"&gt;&lt;i class="r4"&gt;&lt;/i&gt;&lt;span class="value "&gt;15741&lt;/span&gt;&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="carry"&gt; &lt;img class="carry full" title="carry" alt="carry" src="/img/x.gif"/&gt; &amp;#x202d;&amp;#x202d;37500&amp;#x202c;&amp;nbsp;/&amp;nbsp;&amp;#x202d;37500&amp;#x202c;&amp;#x202c; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Arrival&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="in"&gt;in&amp;nbsp;&lt;span class="timer" counting="down" value="85"&gt;0:01:25&lt;/span&gt;&amp;nbsp;hrs.&lt;/div&gt; &lt;div class="at"&gt;&lt;span&gt;at&amp;nbsp;00:43:10&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;a name="at"&gt;&lt;/a&gt; &lt;table cellspacing="1" cellpadding="1" class="troop_details inReturn" &gt; &lt;thead&gt; &lt;tr&gt; &lt;td class="role"&gt; &lt;a href="/karte.php?d=91628"&gt;01] #WorkInProgress&lt;/a&gt; &lt;/td&gt; &lt;td colspan="11" class="troopHeadline"&gt; &lt;a href="/karte.php?d=94829"&gt;Return from 0-New Hulk&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody class="units"&gt; &lt;tr&gt; &lt;th class="coords"&gt; &amp;#x202d;&lt;span class="coordinates coordinatesWrapper coordinatesAligned coordinatesltr"&gt;&lt;span class="coordinateX"&gt;(&amp;#x202d;&amp;minus;&amp;#x202d;1&amp;#x202c;&amp;#x202c;&lt;/span&gt;&lt;span class="coordinatePipe"&gt;|&lt;/span&gt;&lt;span class="coordinateY"&gt;&amp;#x202d;&amp;minus;&amp;#x202d;28&amp;#x202c;&amp;#x202c;)&lt;/span&gt;&lt;/span&gt;&amp;#x202c; &lt;/th&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u21" title="Phalanx: 0:45:33" alt="Phalanx" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u22" title="Swordsman: 0:53:09" alt="Swordsman" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u23" title="Pathfinder: 0:18:46" alt="Pathfinder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u24" title="Theutates Thunder: 0:16:47" alt="Theutates Thunder" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u25" title="Druidrider: 0:19:56" alt="Druidrider" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u26" title="Haeduan: 0:24:32" alt="Haeduan" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u27" title="Ram: 1:19:44" alt="Ram" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u28" title="Trebuchet: 1:46:18" alt="Trebuchet" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u29" title="Chieftain: 1:03:47" alt="Chieftain" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon"&gt; &lt;img class="unit u30" title="Settler: 1:03:47" alt="Settler" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;td class="uniticon last"&gt; &lt;img class="unit uhero" title="Hero" alt="Hero" src="/img/x.gif" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="units last"&gt; &lt;tr&gt; &lt;th&gt;Troops&lt;/th&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit"&gt; 400 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none"&gt; 0 &lt;/td&gt; &lt;td class="unit none last"&gt; 0 &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Bounty&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="res"&gt; &lt;div class="inlineIconList resourceWrapper"&gt;&lt;div class="inlineIcon resources" title="Lumber"&gt;&lt;i class="r1"&gt;&lt;/i&gt;&lt;span class="value "&gt;6130&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Clay"&gt;&lt;i class="r2"&gt;&lt;/i&gt;&lt;span class="value "&gt;5835&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Iron"&gt;&lt;i class="r3"&gt;&lt;/i&gt;&lt;span class="value "&gt;5638&lt;/span&gt;&lt;/div&gt;&lt;div class="inlineIcon resources" title="Crop"&gt;&lt;i class="r4"&gt;&lt;/i&gt;&lt;span class="value "&gt;12397&lt;/span&gt;&lt;/div&gt;&lt;/div&gt; &lt;/div&gt; &lt;div class="carry"&gt; &lt;img class="carry full" title="carry" alt="carry" src="/img/x.gif"/&gt; &amp;#x202d;&amp;#x202d;30000&amp;#x202c;&amp;nbsp;/&amp;nbsp;&amp;#x202d;30000&amp;#x202c;&amp;#x202c; &lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;tbody class="infos"&gt; &lt;tr&gt; &lt;th&gt;Arrival&lt;/th&gt; &lt;td colspan="11"&gt; &lt;div class="in"&gt;in&amp;nbsp;&lt;span class="timer" counting="down" value="920"&gt;0:15:20&lt;/span&gt;&amp;nbsp;hrs.&lt;/div&gt; &lt;div class="at"&gt;&lt;span&gt;at&amp;nbsp;00:57:05&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;</pre><p> 我感興趣的數據如下:</p><ol><li> 從 01-士兵<strong>歸來 00:43:10</strong></li><li> 從 0-新綠巨人<strong>歸來 00:57:05</strong></li></ol><p> 感謝您的建議,這是我目前的代碼:</p><pre> &lt;?php include 'database.php'?&gt; &lt;?php session_start(); ?&gt; &lt;?php include_once('simple_html_dom.php'); $caserma = $_SESSION["caserma"]; $dom = new DOMDocument; libxml_use_internal_errors(true); $dom-&gt;loadHTML($_SESSION["caserma"], LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $texts = []; foreach ($xpath-&gt;query("//table[contains(@class, 'troop_details') and contains(@class, 'inReturn')]//td[@class='troopHeadline']//a[@href]/text()") as $textNode) { $texts[] = $textNode-&gt;nodeValue; } var_export($texts); ?&gt;</pre><p> 但是作為 output 它給了我數組()</p></div>標簽並從特定的文本中提取文本<table> </table> 替換HTML文本中的所有圖像src標簽
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM