简体   繁体   English

PHP正则表达式返回 <option>价值观

[英]PHP regex to return <option> values

Just wondering if you can help me out a bit with a little task I'm trying to do in php. 我只是想知道您是否可以通过一些我想在php中完成的小任务来帮助我。

I have text that looks something like this in a file: 我的文件中的文本看起来像这样:

    (random html)
    ...
    <OPTION VALUE="195" SELECTED>Physical Chem  
    <OPTION VALUE="239">Physical Chem Lab II  
    <OPTION VALUE="555">Physical Chem for Engineers            
    ...
    (random html)

I want to return the # value of the option values ignoring everything else. 我想返回选项值的#值,而忽略其他所有内容。 For example, in the above case I would want 195, 239 & 555 returned, nothing else like "Option Value=". 例如,在上述情况下,我希望返回195、239和555,其他都没有,例如“ Option Value =“。

I am having trouble doing this in PHP. 我在使用PHP时遇到麻烦。 So far I have this: 到目前为止,我有这个:

preg_match("/OPTION VALUE=\"([0-9]*)/", $data, $matches);
        print_r($matches);  

With the return value of this: 用这个的返回值:

Array ( [0] => OPTION VALUE="195[1] => 195) Array ( [0] => OPTION VALUE="195[1] => 195) 数组([0] => OPTION VALUE =“ 195 [1] => 195)数组([0] => OPTION VALUE =” 195 [1] => 195)

How can I return the all the #'s? 我该如何退回所有的#号?

I'm a newbie at pattern matching and tutorials I've read haven't helped much, so thanks a ton! 我是模式匹配的新手,我读过的教程并没有太大帮助,所以非常感谢!

preg_match will return an array containing only the first match. preg_match将返回仅包含第一个匹配项的数组。 The first index of the array wil return a match for the full regular expression, the second one matches the capture group in the parentheses, try the following to get a concept of how this works: 数组的第一个索引将返回完整正则表达式的匹配项,第二个索引将匹配括号中的捕获组,请尝试以下操作以了解其工作原理:

preg_match("/(OPTION) VALUE=\"([0-9]*)/", $data, $matches);
    print_r($matches);

You will see that it outputs the following: 您将看到它输出以下内容:

Array
(
    [0] => OPTION VALUE="195
    [1] => OPTION
    [2] => 195
)

Array[0] contains data of the full match, array [1] contains data from the first capture group (OPTION) and array[2] contains data from the second capture group ([0-9]*). 数组[0]包含完全匹配的数据,数组[1]包含来自第一个捕获组(OPTION)的数据,数组[2]包含来自第二个捕获组([0-9] *)的数据。

In order to match more than one occurrence, you need to use the preg_match_all function. 为了匹配多个事件,您需要使用preg_match_all函数。 If we apply this to your original code like so: 如果我们将其应用到您的原始代码中,如下所示:

preg_match_all("/OPTION VALUE=\"([0-9]*)/", $data, $matches);
    print_r($matches);

We get: 我们得到:

Array
(
    [0] => Array
        (
            [0] => OPTION VALUE="195
            [1] => OPTION VALUE="239
            [2] => OPTION VALUE="555
        )

    [1] => Array
        (
            [0] => 195
            [1] => 239
            [2] => 555
        )

)

I hope this makes things clear! 我希望这可以使事情变得清楚!

I think you've done it right. 我认为您做对了。 PHP returns the full match in [0], and then the captured groups (parenthases) as the others. PHP会在[0]中返回完全匹配项,然后将捕获的组(括号)作为其他组。

Check this out: http://xrg.es/#15m7krv 检查一下: http : //xrg.es/#15m7krv

Try this: 尝试这个:

preg_match_all('/OPTION VALUE=\\"([0-9])+\\"/', $data, $matches);

Edit 编辑

Misunderstood your question. 误解了你的问题。 Changed to preg_match_all() 更改为preg_match_all()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM