将PHP的eregi更改为preg_match

时间:2014-02-03 05:03:18

标签: php wamp

我不知道如何修复此错误

 Warning: preg_match(): Unknown modifier '[' in 

我的代码是

while(list($k,$v)=each($con2)) {
    $patt="($this->block_start_word|$this->block_end_word)[[:blank:]]*([0-9a-zA-Z\_]+)[[:blank:]]*$this->block_end_delim(.*)";
    if (eregi($patt,$v,$res)) {

我想将eregi的php版本更新为preg_match,我就是这个

while(list($k,$v)=each($con2)) {
    $patt="($this->block_start_word|$this->block_end_word)[[:blank:]]*([0-9a-zA-Z\_]+)[[:blank:]]*$this->block_end_delim(.*)";
    if ( preg_match($patt,$v,$res)) {

3 个答案:

答案 0 :(得分:1)

正如您在此回答中所看到的,preg_match将第一个字符视为分隔符 How to change PHP's eregi to preg_match

具体而言,您会收到错误,因为preg_match使用'('作为分隔符,因此在($ this-> block_start_word | $ this-> block_end_word)之后结束模式并且'['

上的错误

将图案更改为 $ PATT =“/($这 - &GT; block_start_word | $这 - &GT; block_end_word)[[:空白:]] ([0-9A-ZA-Z _] +)[[:空白:]] < / EM> $这 - &GT;(。*)block_end_delim /“;

它应该有效,Goodluck!

答案 1 :(得分:0)

好的,我们来看看这个正则表达式:

"($this->block_start_word|$this->block_end_word)[[:blank:]]*([0-9a-zA-Z\_]+)[[:blank:]]*$this->block_end_delim(.*)"

我认为你想要:

  • 文字字符串$this->block_start_word$this->block_end_word
  • 后跟0个或更多空白字符
  • 后跟一个或多个字母数字字符
  • 后跟0个或更多空白
  • 后跟文字字符串$this->block_end_delim
  • 直到行尾

那个,怎么样

<?php
$handle = fopen('php://stdin', "r");
while (($line = fgets($handle, 4096)) !== false) {
        $exp  = '/';
        $exp .= '(';
        $exp .= '\$this\-\>block_start_word';
        $exp .= '|';
        $exp .= '\$this\-\>block_end_word';
        $exp .= ')';
        $exp .= '\s*'; // Like [[:blank:]]*
        $exp .= '([0-9a-zA-Z\_]+)';
        $exp .= '\s*'; // Like [[:blank:]]*
        $exp .= '\$this\-\>block_end_delim';
        $exp .= '(.*)/';
        if(preg_match($exp,$line)) {
                print $line;
        }
}
?>

如果{1}},$this->block_start_word$this->block_end_word设置在PHP脚本的其他位置:

$this->block_end_delim

答案 2 :(得分:0)

使用preg_match您应该定义分隔符,并且最好还包括preg_quote

$patt="/(".preg_quote($this->block_start_word)."|".preg_quote($this->block_end_word."))[[:blank:]]*([0-9a-zA-Z\_]+)[[:blank:]]*".preg_quote($this->block_end_delim)."(.*)/";