PHP正则表达式 - 在这种情况下我现在做错了什么?

问题描述:

using the regular expression:

$pattern = "/adminvalues = \"\([^']+, '[^']+', MD5\('[^']+'\), '[^']+','[^']+','[^']+',[^']+,[^']+\)\";/";
preg_match($pattern, $data, $results);
print_r($results);

searching the following text (values within brackets can change... hence the need for regex...):

adminvalues = "(NULL, 'Admin12345', 'MD5(Admin12345)', 'Admin12345', 'Admin12345', 5, 0, 0)";

so am looking for a pattern (in reg-ex) that matches essentially the following (I think...):

[adminvalues = "(] [any character] [, '] [any character] [', MD5('] [any character] ['), '] [any character] [', '] [any character]  [', ] [any character] [, ] [any character] [, ] [any character] [)";]

Where: Any character is one or more of any combination of (letter / number / symbol)

I get no matches using the PHP code at the top - so I've obviously done something wrong! I just can't spot the error myself. (I only started using regex this morning really...) What have I done wrong? :)

Solve: (Thanks to Amal Murali) Using htmlspecialchars() when reading the php script being used as $data (rather than just to display contents)

使用正则表达式: p>

  $ pattern =“/  adminvalues = \“\([^'] +,'[^'] +',MD5 \('[^'] +'\),'[^'] +','[^'] +','  [^'] +',[^'] +,[^'] + \)\“; /”; 
preg_match($ pattern,$ data,$ results); 
print_r($ results); 
  代码>  pre> 
 
 

搜索以下文本(括号内的值可以更改...因此需要正则表达式...): p>

   adminvalues =“(NULL,'Admin12345','MD5(Admin12345)','Admin12345','Admin12345',5,0,0)”; 
  code>  pre> 
 
 

所以我正在寻找一种模式(在reg-ex中)基本上匹配以下(我认为...): p>

  [adminvalues =“([[any character] [  ,'] [任何字符] [',MD5('] [任何字符] ['),'] [任何字符] [','] [任何字符] [',] [任何字符] [,] [任何 字符] [,] [任何字符] [)“;] 
  code>  pre> 
 
 

其中:任何字符都是(字母/数字的任意组合中的一个或多个) /符号) strong> p>

我没有使用PHP代码获得匹配 顶部 - 所以我显然做错了! 我自己无法发现错误。 (我今天早上才真正开始使用正则表达式...)我做错了什么? :) p>

解决: b>(感谢Amal Murali) 当读取用作 $ data code>的php脚本时,使用htmlspecialchars() (而不仅仅是显示内容) p> div>

Try the following regex:

$pattern = <<<PATTERN
~
  adminvalues\s*=\s*
  "
  \(                  
    [^,]+,\s*
    '[^']+',\s*
    MD5\('[^']+'\),\s*
    '[^']+',\s*
    '[^']+',\s*
    \d,\s*
    \d,\s*
    \d\s*
  \)
  ";
~xm
PATTERN;

Regex101 Demo