PHP正则表达式子字符串

时间:2013-03-16 10:20:29

标签: php regex substring

<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>

我们如何在&gt;中提取每个字符串&lt;选项标签?

1 个答案:

答案 0 :(得分: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 “<” literally
)
[^>]    # Match any character that is NOT a “>”
   +       # 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 “>” literally
)
"

<击>

修改

也许你可以选择DOM而不是RegExp

<?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;
}
?>

访问here了解详细信息。希望这会有所帮助。