有效的正则表达式在PHP中不那么有效

时间:2017-08-03 19:45:59

标签: php regex validation

所以我想在表单标签之间提取所有内容(包括自己的标签)。

表格如下:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if([keyPath isEqualToString:@"selectedTextRange"] && self.txtfield == object)
        NSLog(@"cursor moved");
}

我正在使用的正则表达式是:<body><br /> <!-- <form method="POST" action="#"> <table style="table-layout: fixed; border: 1px solid #ffffff; " border="1"> <!-- <col width="50"> --></p> <tr style="width: 1154px; background-color: #0d56c2; vertical-align: middle; color: #ffffff; height: 70px; "> <td style="width: 413px; text-align: center;">Calls</td> <td style="background-color: #D6DCE5; width: 319px; padding-left: 20px; padding-top: 15px;"><input type="text" name="calls" value="150" style="width: 173px;"></td> <td style="width: 412px; padding: 5px; vertical-align: middle;"> in a period of <input name="period" value="5" style="width: 173px; ">&nbsp;<br /> <select name="callUnit" style="width: 100px; height: 29px; position: absolute;"><option value="hour" selected>hours</option><option value="minute" >minutes</option></select> </td> </tr> </table> </form> </body> 并且根据regex101这是一个有效的正则表达式,应该提取表单标签+两者之间的翻转。

但是在preg_match中使用上面的正则表达式会出现以下错误:<form.*>[\s\S]*<\/form>

1 个答案:

答案 0 :(得分:1)

不确定您的实际问题是什么。对我来说,你的模式就像魅力一样:

echo GridView::widget([
            'dataProvider' => $dataProvider,
            'pjax'=>true,
            'columns' => [
                [
                    'attribute' => 'brand_name',
                    'value' => function($model,$key,$idx,$col){
                            return $model['brand_name'];
                        },
                ],
                [
                    'class'=>'kartik\grid\EditableColumn',
                    'value' => function($model,$row){
                            //return $model->brand_name;
                            return $model->getMonthBudget(new DateTime('2017-01'), [11311])['budget_net']; // TODO - Testingv
                        },
                    'editableOptions' => [
                        'name' => 'it requires this without attribute',
                        'header'=>'Jan',
                        'inputType'=>\kartik\editable\Editable::INPUT_SPIN,
                        'options'=>['pluginOptions'=>['min'=>0, 'max'=>5000]],
                        'pluginEvents' => [
                            "editableSuccess"=>"function(event, val, form, data) { console.log(data); }",
                        ]
                    ]
                ],
                [
                    'attribute' => 'Feb',
                    'value' => function($model,$key,$idx,$col){
                            //return $model->brand_name;
                            return $model->getMonthBudget(new DateTime('2017-02'), [11311])['budget_net']; // TODO - Testing
                        },
                ],
                [
                    'attribute' => 'Mar',
                    'value' => function($model,$key,$idx,$col){
                            //return $model->brand_name;
                            return $model->getMonthBudget(new DateTime('2017-03'), [11311])['budget_net']; // TODO - Testing
                        },
                ],

输出是:

<?php
$markup = <<<HTML
<body><br />
    <!--
<form method="POST" action="#">
<table style="table-layout: fixed; border: 1px solid #ffffff; " border="1">
            <!--
<col width="50"> --></p>
<tr style="width: 1154px; background-color: #0d56c2; vertical-align: middle; color: #ffffff; height: 70px; ">
<td style="width: 413px; text-align: center;">Calls</td>
<td style="background-color: #D6DCE5; width: 319px; padding-left: 20px; padding-top: 15px;"><input type="text" name="calls" value="150" style="width: 173px;"></td>
<td style="width: 412px; padding: 5px; vertical-align: middle;"> in a period of <input name="period" value="5" style="width: 173px; ">&nbsp;<br />
                    <select name="callUnit" style="width: 100px; height: 29px; position: absolute;"><option value="hour" selected>hours</option><option value="minute" >minutes</option></select>
                </td>
</tr>
</table>
</form>
</body>
HTML;

preg_match('~<form.*>([\s\S]*)</form>~', $markup, $tokens);
var_dump($tokens[1]);

我做的唯一修改是添加捕获组(string(829) " <table style="table-layout: fixed; border: 1px solid #ffffff; " border="1"> <!-- <col width="50"> --></p> <tr style="width: 1154px; background-color: #0d56c2; vertical-align: middle; color: #ffffff; height: 70px; "> <td style="width: 413px; text-align: center;">Calls</td> <td style="background-color: #D6DCE5; width: 319px; padding-left: 20px; padding-top: 15px;"><input type="text" name="calls" value="150" style="width: 173px;"></td> <td style="width: 412px; padding: 5px; vertical-align: middle;"> in a period of <input name="period" value="5" style="width: 173px; ">&nbsp;<br /> <select name="callUnit" style="width: 100px; height: 29px; position: absolute;"><option value="hour" selected>hours</option><option value="minute" >minutes</option></select> </td> </tr> </table> " )以便能够实际提取某些内容。

您正在使用反斜杠转义结束(...)标记中的正斜杠。最有可能的原因是,像</form>这样的在线正则表达式工具在其模式中使用正斜杠作为标准分隔符。请注意,您可以使用其他字符使图案更易于阅读,因为您必须转义字符然后...

怀疑你可能忘了把你的模式放在分隔符之间?