[英]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 我想将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)) {
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 如您在此答案中所见,preg_match将第一个字符视为定界符如何将PHP的eregi更改为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 '[' 具体来说,您会收到错误消息,因为preg_match使用'('作为分隔符,因此在($ this-> block_start_word | $ this-> block_end_word)和'['
Change the pattern to $patt="/($this->block_start_word|$this->block_end_word)[[:blank:]] ([0-9a-zA-Z_]+)[[:blank:]] $this->block_end_delim(.*)/"; 将模式更改为$ patt =“ /($ this-> block_start_word | $ this-> block_end_word)[[:blank:]] ([0-9a-zA-Z _] +)[[:blank:]] $ this - > block_end_delim(。*)/“;
And it should work, Goodluck! 它应该起作用,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: 我想你要:
$this->block_start_word
or $this->block_end_word
文字字符串$this->block_start_word
或$this->block_end_word
$this->block_end_delim
后跟文字字符串$this->block_end_delim
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: 如果$this->block_start_word
, $this->block_end_word
和$this->block_end_delim
在PHP脚本的其他位置设置:
<?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)."(.*)/";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.