简体   繁体   中英

change PHP's eregi to preg_match

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

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!

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

使用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)."(.*)/";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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