簡體   English   中英

php中的preg_match用於選擇標簽

[英]preg_match in php for select tags

我想使用preg_match獲得以下選擇標記之間的所有選項值

<select id="current-statement" name="current-statement" data-reactid=".4.2.2.1.0.2.1.1.0">
<option value="current" data-reactid=".4.2.2.1.0.2.1.1.0.0">Current Statement</option>
<option value="90989266-c853-289b-dfea-3cdfe2213db7" data-reactid=".4.2.2.1.0.2.1.1.0.1">1st Statement</option>
<option value="165eb5ea-fd48-53c8-020b-6e3287859922" data-reactid=".4.2.2.1.0.2.1.1.0.2">second statement</option>
<option value="0d558fa0-8f48-afa2-7a9a-e8f85fbbbc42" data-reactid=".4.2.2.1.0.2.1.1.0.3">third statement</option>
<option value="9c78f8aa-3b09-4574-1c10-8f450b45eb5b" data-reactid=".2.0.0.1.0.2.1.1.0.4">4th statement</option>
</select>

我試圖讓它像跟隨preg_match但無法這樣做

preg_match("'<select id=\"current-statement\" name=\"current-statement\" data-reactid=\".4.2.2.1.0.2.1.1.0\">(.*?)</select>'", $content, $match);

if($match) echo "result=".$match[1];

請幫忙

使用DOM,任務簡化為編寫一個可用的XPath表達式:

//select/*/text()

哪里

  • //select - 查找所有select標簽
    • /* - 然后是里面的任何孩子
      • /text() - 並獲取文本節點。

請參閱PHP演示

$html = <<<DATA
<select id="current-statement" name="current-statement" data-reactid=".4.2.2.1.0.2.1.1.0">
<option value="current" data-reactid=".4.2.2.1.0.2.1.1.0.0">Current Statement</option>
<option value="90989266-c853-289b-dfea-3cdfe2213db7" data-reactid=".4.2.2.1.0.2.1.1.0.1">1st Statement</option>
<option value="165eb5ea-fd48-53c8-020b-6e3287859922" data-reactid=".4.2.2.1.0.2.1.1.0.2">second statement</option>
<option value="0d558fa0-8f48-afa2-7a9a-e8f85fbbbc42" data-reactid=".4.2.2.1.0.2.1.1.0.3">third statement</option>
<option value="9c78f8aa-3b09-4574-1c10-8f450b45eb5b" data-reactid=".2.0.0.1.0.2.1.1.0.4">4th statement</option>
</select>
DATA;

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xpath = new DOMXPath($dom);
$opts = $xpath->query('//select/*/text()');
$res = array();
foreach($opts as $opt) { 
   array_push($res, $opt->nodeValue);
}
print_r($res);

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM