将字符串拆分为子字符串

时间:2014-03-01 23:21:24

标签: php regex arrays string split

在用旧版PHP编写的自动化脚本中,我得到了以下字符串

string '<br /><select id="addid8231736" name="addid8231736" size="1" >
<option value="" selected="true">Select an option</option>
<option value="11518065" data-qty="5"> 11 - PKR11099.59</option>
<option value="11518063" data-qty="1"> 9 - PKR9850.00</option>' 
(length=246)

这个字符串将是一个可变长度和可变选项字符串,我最终希望它能从中获取选项

价格

  • PKR11099.59

  • PKR9850.00

size ids

  • 11
  • 9

SO FAR 我成功地拥有了这个

string 'Select an option 11 - PKR11099.59 9 - PKR9850.00' (length=48)

通过字符串标签..

有人可以帮我找到我想要的选项吗?

谢谢你们所有人..

更新

我试过通过explode("</option><option", $options_data1);爆炸它 得到了这个

array (size=3)
  0 => string '<br/><select id="addid8231736" name="addid8231736" size="1"><option value="" selected="true">Select an option' (length=109)
  1 => string ' value="11518065" data-qty="5"> 11 - PKR11099.59' (length=48)
  2 => string ' value="11518063" data-qty="1"> 9 - PKR9850.00</option>' (length=55)

1 个答案:

答案 0 :(得分:1)

$string = <<<EOS
<br /><select id="addid8231736" name="addid8231736" size="1" >
<option value="" selected="true">Select an option</option>
<option value="11518065" data-qty="5"> 11 - PKR11099.59</option>
<option value="11518063" data-qty="1"> 9 - PKR9850.00</option>
EOS;
preg_match_all('~<option.*?>\s*(\d+)\s*-\s*(.*?)</option>~',$string,$matches);

输出:

Array
(
    [0] => Array
        (
            [0] => <option value="11518065" data-qty="5"> 11 - PKR11099.59</option>
            [1] => <option value="11518063" data-qty="1"> 9 - PKR9850.00</option>
        )

    [1] => Array
        (
            [0] => 11
            [1] => 9
        )

    [2] => Array
        (
            [0] => PKR11099.59
            [1] => PKR9850.00
        )

)