我從 XML 檔案中得到了可能的資料。我只需要從陣列中的字串中輸出 URL。像“https://d1.cloudfront.net/00722.jpg”這樣的格式,沒有其他標簽和樣式。我已經用 preg_match_all 嘗試過,但我沒有得到任何結果。我做錯了什么?
public function xmlParserPICtn():string
{
$valuesPICtn = $this->xml->xpath("//OBJEKT[@ID='91727']//PICTURE");
$searchpattern="@SRC=(.*)width@";
preg_match_all($searchpattern, $valuesPICtn, $valuesPICt); //Search-String
foreach ($valuesPICt as $PICelements)
{
$display .= '<li>';
$display .= ''.$PICelements->PIC.'';
$display .= '</li>';
}
$display .= '';
return $display;
}
<?xml version="1.0" encoding="utf-8"?>
<OBJEKT ID="91727">
<PICTURE ID="7">
<ID>7</ID>
<PIC><IMG SRC="https://d1.cloudfront.net/00722.jpg" width="610" height="480" BORDER=0></PIC>
</PICTURE>
<PICTURE ID="11">
<ID>11</ID>
<PIC><IMG SRC="https://d1.cloudfront.net/01123.jpg" width="630" height="480" BORDER=0></PIC>
</PICTURE>
<PICTURE ID="2">
<ID>2</ID>
<PIC><IMG SRC="https://d1.cloudfront.net/00224.jpg" width="740" height="480" BORDER=0></PIC>
</PICTURE>
<PICTURE ID="9">
<ID>9</ID>
<PIC><IMG SRC="https://d1.cloudfront.net/00925.jpg" width="940" height="480" BORDER=0></PIC>
</PICTURE>
</OBJEKT>
uj5u.com熱心網友回復:
試試這個正則運算式:
(?<=SRC=")(.*?)(?=\")
我只得到沒有其他標簽的 URL。
你在這里找到了演示
uj5u.com熱心網友回復:
您應該回圈 xpath 查詢的結果 valuesPICtn
然后對于回圈中的每個專案$PICelements->PIC,都有一張圖片。您可以改用 preg_match,并采用組 1 值。請注意,preg_match和preg_match_all的第二個引數采用字串,您嘗試在代碼中傳遞 xpath 呼叫的回傳值。
請注意,代碼中的這部分$display .= '';可以省略,因為它連接了一個空字串。
該模式SRC="([^"] )"是一個稍微更新的版本,匹配 SRC=" 并在組 1 中捕獲除雙引號之外的任何字符
public function xmlParserPICtn():string
{
$valuesPICtn = $this->xml->xpath("//OBJEKT[@ID='91727']//PICTURE");
foreach ($valuesPICtn as $PICelements)
{
$searchpattern='@SRC="([^"] )"@';
preg_match($searchpattern, $PICelements->PIC, $valuesPICt); //Search-String
$display .= '<li>';
$display .= $valuesPICt[1];
$display .= '</li>';
}
return $display;
}
php 演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/374952.html
