2

I dont know how to fixe this error

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

my code is

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)) {

I want to update php version of eregi to preg_match and I tru this

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)) {
hakre
  • 193,403
  • 52
  • 435
  • 836
user3264544
  • 21
  • 1
  • 3

3 Answers3

1

As you can see in this answer preg_match treats the first character as a delimiter How to change PHP's eregi to preg_match

Specifically you get the error because preg_match uses '(' as the delimiter and thus ends the pattern after ($this->block_start_word|$this->block_end_word) and errors on '['

Change the pattern to $patt="/($this->block_start_word|$this->block_end_word)[[:blank:]]([0-9a-zA-Z_]+)[[:blank:]]$this->block_end_delim(.*)/";

And it should work, Goodluck!

Community
  • 1
  • 1
clancer
  • 613
  • 4
  • 10
0

OK, let's go over this regex:

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

I presume you want:

  • either the literal string $this->block_start_word or $this->block_end_word
  • followed by 0 or more blankspace characters
  • followed by 1 or more alphanumeric characters
  • followed by 0 or more blankspaces
  • followed by the literal string $this->block_end_delim
  • until the end of the line

That in mind, how about

<?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;
        }
}
?>

If $this->block_start_word, $this->block_end_word, and $this->block_end_delim are set elsewhere in the PHP script:

<?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;
        }
}
?>
samiam
  • 553
  • 3
  • 6
0

With preg_match you should define delimiters and you'd better include preg_quote as well:

$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)."(.*)/";
revo
  • 47,783
  • 14
  • 74
  • 117