2011-05-16 43 views
1
$options = '<select name="extra2" id="extra2" class="select_smaller"> 
    <option value="Algema">Algema</option> 
    <option value="Barkas">Barkas</option> 
. 
. 
. 
</select>'; 

,這是我的代碼簡單的regex我除了現有的代碼

/** 
* Return HTML of select field with one option selected, built based 
* on the list of options provided 
* @param mixed $options array of options or HTML of select form field 
* @return string HTML of the select field 
*/ 
function makeSelect($name, $options) { 
if (is_string($options)) { 
    // assuming the options string given is HTML of select field 
    $regex = '/<option value=\"([a-zA-Z0-9]*)\"\>/'; 
    $count = preg_match_all($regex, $options, $matches); 
    if ($count) { 
     $options = $matches[1]; 
    } else { 
     $options = array(); 
    } 
} 

foreach ($options as &$option) { 
    $selected = isset($_GET[$name]) && $_GET[$name] == $option; 
    $option = sprintf('<option value="%1$s"%2$s>%1$s</option>', 
         htmlspecialchars($option), 
         $selected ? ' selected="selected"' : null); 
} 

return sprintf('<select name="%1$s" id="%1$s" class="select">%2$s</select>', 
       htmlspecialchars($name), 
       join($options)); 
} 

echo makeSelect('extra2', $options); 

我怎樣才能使用正則表達式,而不是手動選擇列表寫它(extra2)的名字?

+0

你可以是一個小更清楚你試圖完成什麼?在我看來,你想要使用正則表達式來解析一段HTML,以捕獲選擇輸入中可用選項的名稱?爲什麼在PHP中有一個HTML字符串呢?你從HTML獲取? – 2011-05-16 13:59:59

+0

我有500個表格,每個表格有2-3個下拉列表。我無法一一編輯它們,因此這段代碼將幫助我生成並顯示正確的結果,以便根據url數據選擇選項值。該代碼工作正常,除了我需要手動添加名稱在他們每個人的事實。當我有解決方案時,我會用大量搜索和替換程序來替換所有表單。 – EnexoOnoma 2011-05-16 14:09:18

+0

讓我重新解釋一下這個問題。 $ options變量的內容來自哪裏?你是自己製作弦還是從別的地方得到它? – 2011-05-16 14:19:29

回答

0

嘗試類似:

<?php 
function selectOption($htmlSelect, array $data = array()) { 
    $result = $htmlSelect; 

    $dom = new DOMDocument(); 
    if ($dom->loadXml($htmlSelect)) { 
     $selectName = $dom->documentElement->getAttribute('name'); 
     if (isset($data[$selectName])) { 
      $xpath = new DOMXPath($dom); 

      $optionNodeList = $xpath->query('//option[@value="' . $data[$selectName] . '"]'); 
      if ($optionNodeList->length == 1) { 
       $optionNodeList->item(0)->setAttribute('selected', 'selected'); 

       $result = $dom->saveXml($dom->documentElement, LIBXML_NOEMPTYTAG); 
      } 
     } 
    } 

    return $result; 
} 

$htmlSelect = '<select name="extra2" id="extra2" class="select_smaller"> 
    <option value="Algema">Algema</option> 
    <option value="Barkas">Barkas</option> 
</select>'; 

echo selectOption(
    $htmlSelect, 
    array('extra2' => 'Barkas') // could be $_GET, $_POST or something else 
)); 

我想你必須添加一些錯誤檢查在這裏和那裏。

+0

謝謝,但不是我在找什麼。我需要編輯上面的代碼。 – EnexoOnoma 2011-05-16 14:22:22

+1

當然,這不能代替你的代碼,但你可以在適當的地方添加它,或者類似的東西。由於你的問題相當模糊,我無法舉一個更好的例子。 – Yoshi 2011-05-16 14:24:12

+0

你能和我一起編輯我的代碼嗎? – EnexoOnoma 2011-05-16 14:36:22

0

不要讓該字符串開頭。在連接到一個字符串之前處理你所擁有的數據。

最有可能的是,在處理數據之後,你也不需要做一個巨大的字符串。你不能運行一個循環並回顯HTML,需要放入變量嗎?

0

如果你確定$選項的結構,你可以這樣做:

function makeSelect($name, $options) { 
    if (is_string($options)) { 
     preg_match('/<select name="(.+?)"/', $options, $match); 
     $name = $match[1]; 
     ... 

你也可以建立$選項包含nameof選擇這種方式:

$options = array(
    'name' => 'The name', 
    'options => ... 
);