正则表达式匹配HTML选项的内容

NB: Please use DOM parsers if you are able to use them in your project, use regex only on edge cases.

I need to get an array with the content of each option, this is my HTML:

<option value="2" selected>none</option>
<option value="1">fronttext</option>
<option value="15">fronttext,frontpicture</option>

I need to get:

["none", "fronttext", "fronttext,frontpicture"]

I'm using this regex:

<option.*>(.*)<\/option> 

But when I use it in PHP with:

preg_match_all('/<option.*>(.*)<\/option>/', $string, $matches);

It matches only the last result ("fronttext,frontpicture").

What am I doing wrong?

You could use the below regex,

<option.*?>\K.*?(?=<\/option>)

DEMO

Code would be,

preg_match_all('~<option.*?>\K.*?(?=<\/option>)~', $string, $matches);

Example:

<?php
$mystring = <<<'EOT'
<option value="2" selected>none</option>
<option value="1">fronttext</option>
<option value="15">fronttext,frontpicture</option>
EOT;
preg_match_all('~<option.*?>\K.*?(?=<\/option>)~', $mystring, $matches);
print_r($matches);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => none
            [1] => fronttext
            [2] => fronttext,frontpicture
        )

)

It is because you are using .* in your regex which is by nature greedy.

Try this regex:

preg_match_all('~<option[^>]*>([^<]*)</option>~', $string, $matches);

However as a word of caution I suggest you to look into using DOM parser instead of regex for parsing HTML/XML text.