0
<select name="states">
    <option value="">--  Select State / Province  --</option>
    <option value="1">Alabama</option><option value="2">Alaska</option>
    <option value="4">Arizona</option><option value="3">Arkansas</option>
    <option value="5">California</option><option value="6">Colorado</option>
    <option value="7">Connecticut</option>
    <option value="8">Delaware</option>
    <option value="9">District Of Columbia</option>
    <option value="10">Florida</option>
    <option value="11">Georgia</option><option value="12">Hawaii</option>
    <option value="13">Idaho</option>
    <option value="14">Illinois</option><option value="16">Indiana</option>
    <option value="15">Iowa</option>
    <option value="17">Kansas</option><option value="18">Kentucky</option>
    <option value="19">Louisiana</option>
    <option value="20">Maine</option>
    <option value="21">Maryland</option>
    <option value="23">Massachusetts</option>
    <option value="22">Michigan</option><option value="25">Minnesota</option>
    <option value="24">Mississippi</option>
    <option value="26">Missouri</option><option value="27">Montana</option>
    <option value="28">Nebraska</option><option value="39">Nevada</option>
    <option value="29">New Hampshire</option>
    <option value="30">New Jersey</option><option value="31">New Mexico</option>
    <option value="32">New York</option>
    <option value="33">North Carolina</option>
    <option value="34">North Dakota</option>
    <option value="35">Ohio</option><option value="36">Oklahoma</option>
    <option value="37">Oregon</option>
    <option value="38">Pennsylvania</option>
    <option value="40">Rhode Island</option>
    <option value="41">South Carolina</option>
    <option value="42">South Dakota</option>
    <option value="43">Tennessee</option>
    <option value="44">Texas</option>
    <option value="45">Utah</option>
    <option value="46">Vermont</option>
    <option value="47">Virginia</option>
    <option value="48">Washington</option>
    <option value="49">West Virginia</option>
    <option value="50">Wisconsin</option><option value="51">Wyoming</option>
</select>

オプションタグの><内の各文字列をどのように抽出できますか?

4

1 に答える 1

1

これを試して

preg_match_all('/(?<=<)[^>]+(?=>)/m', $subject, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
    # Matched text = $result[0][$i];
}

説明

"
(?<=    # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
   <       # Match the character “&lt;” literally
)
[^>]    # Match any character that is NOT a “&gt;”
   +       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?=     # Assert that the regex below can be matched, starting at this position (positive lookahead)
   >       # Match the character “&gt;” literally
)
"

編集

おそらく、正規表現ではなくDOMを選択するかもしれません

<?php
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<select name="states">
    <option value="">--  Select State / Province  --</option>
    <option value="1">Alabama</option>
    <option value="2">Alaska</option>
    <option value="4">Arizona</option>
    <option value="3">Arkansas</option>
    <option value="5">California</option>
    <option value="6">Colorado</option>
    <option value="7">Connecticut</option>
    <option value="8">Delaware</option>
    <option value="9">District Of Columbia</option>
    <option value="10">Florida</option>
    <option value="11">Georgia</option>
    <option value="12">Hawaii</option>
    <option value="13">Idaho</option>
    <option value="14">Illinois</option>
    <option value="16">Indiana</option>
    <option value="15">Iowa</option>
    <option value="17">Kansas</option>
    <option value="18">Kentucky</option>
    <option value="19">Louisiana</option>
    <option value="20">Maine</option>
    <option value="21">Maryland</option>
    <option value="23">Massachusetts</option>
    <option value="22">Michigan</option>
    <option value="25">Minnesota</option>
    <option value="24">Mississippi</option>
    <option value="26">Missouri</option>
    <option value="27">Montana</option>
    <option value="28">Nebraska</option>
    <option value="39">Nevada</option>
    <option value="29">New Hampshire</option>
    <option value="30">New Jersey</option>
    <option value="31">New Mexico</option>
    <option value="32">New York</option>
    <option value="33">North Carolina</option>
    <option value="34">North Dakota</option>
    <option value="35">Ohio</option>
    <option value="36">Oklahoma</option>
    <option value="37">Oregon</option>
    <option value="38">Pennsylvania</option>
    <option value="40">Rhode Island</option>
    <option value="41">South Carolina</option>
    <option value="42">South Dakota</option>
    <option value="43">Tennessee</option>
    <option value="44">Texas</option>
    <option value="45">Utah</option>
    <option value="46">Vermont</option>
    <option value="47">Virginia</option>
    <option value="48">Washington</option>
    <option value="49">West Virginia</option>
    <option value="50">Wisconsin</option>
    <option value="51">Wyoming</option>
</select>
XML;

$dom = new DOMDocument;
$dom->loadXML($xml);
$options = $dom->getElementsByTagName('option');
foreach ($options as $option) {
    echo $option->nodeValue, PHP_EOL;
}
?>

詳細については、こちらをご覧ください。お役に立てれば。

于 2013-03-16T10:25:17.190 に答える