从PHP中的preg_split获取分隔符

时间:2021-08-10 22:06:49

My PHP code:

我的PHP代码:

$cmd = 'SELECT var1 FROM var2 GROUP BY var3 LIMIT var4';
// split command
$p = preg_split('/SELECT|FROM|GROUP BY|LIMIT/si', $cmd);

Output:

输出:

Array
(
  [0] =>  
  [1] =>  var1 
  [2] =>  var2 
  [3] =>  var3
  [4] =>  var4
)

Is that output below possible?

以下输出可能吗?

Array
(
  [SELECT] => var1
  [FROM] => var2
  [GROUP BY] => var3
  [LIMIT] => var4
)

Or is there any way to get delimiters?

或者有没有办法获得分隔符?

4 个解决方案

#1


2  

$cmd = 'SELECT var1 FROM var2 GROUP BY var3 LIMIT var4';

$array = array();
if(preg_match_all('/(SELECT|FROM|GROUP BY|LIMIT)\s+([^\s]+)/si', $cmd, $matches)) {
    $array = array_combine($matches[1], $matches[2]);
}

var_dump($array);

Result:

结果:

array
  'SELECT' => string 'var1' (length=4)
  'FROM' => string 'var2' (length=4)
  'GROUP BY' => string 'var3' (length=4)
  'LIMIT' => string 'var4' (length=4)

#2


3  

Have you considered using a SQL parser? You're bound to get wrong results with this regex-hack.

您是否考虑过使用SQL解析器?这个正则表达式攻击你一定会得到错误的结果。

Checkout:

查看:

  1. http://code.google.com/p/php-sql-parser
  2. http://code.google.com/p/php-sql-parser
  3. http://pear.php.net/package/SQL_Parser
  4. http://pear.php.net/package/SQL_Parser
  5. PHP MySQL SQL parser (INSERT and UPDATE)
  6. PHP MySQL SQL解析器(INSERT和UPDATE)

A demo with option 1:

带选项1的演示:

#!/usr/bin/env php
<?php

// http://php-sql-parser.googlecode.com/svn/trunk/php-sql-parser.php
require_once('php-sql-parser.php');

$cmd = 'SELECT var1 FROM var2 GROUP BY var3 LIMIT var4';
$parser = new PHPSQLParser($cmd);
$parse_tree = $parser->parsed;

echo 'SELECT -> ' . $parse_tree['SELECT'][0]['base_expr'] . "\n";
echo 'FROM   -> ' . $parse_tree['FROM'][0]['table'] . "\n";
echo 'GROUP  -> ' . $parse_tree['GROUP'][0]['base_expr'] . "\n";
echo 'LIMIT  -> ' . $parse_tree['LIMIT']['end'] . "\n";

?>

produces:

生产:

SELECT -> var1
FROM   -> var2
GROUP  -> var3
LIMIT  -> var4

#3


1  

Try

尝试

$matches = array();
preg_match ('~^SELECT (?P<SELECT>.+) FROM (?P<FROM>.+) GROUP BY (?P<GROUPBY>.+) LIMIT (?P<LIMIT>.+)~', $cmd, $matches);

Or is there any way to get delimiters?

或者有没有办法获得分隔符?

Regular expressions always starts with their delimiters (because its the other way round: The first character defines the delimiter)

正则表达式总是从它们的分隔符开始(因为它反过来:第一个字符定义分隔符)

echo $regEx[0];

#4


1  

a bit messy...

有点乱......

//echo file_get_contents('http://wp.me/pbZy8-1WM');
$cmd = 'SELECT var1 FROM var2 GROUP BY var3 LIMIT var4';
// split command
$pattern = 'SELECT|FROM|GROUP BY|LIMIT';
$p = preg_split('/('.$pattern.')/si', $cmd, null,PREG_SPLIT_DELIM_CAPTURE);
foreach($p as $i => $str)
if (strpos($pattern,$str) !== false)
{
    $rez[$str] = $p[$i+1];
} 
var_dump($rez);

gives:

得到:

  'SELECT' => string ' var1 ' (length=6)
  'FROM' => string ' var2 ' (length=6)
  'GROUP BY' => string ' var3 ' (length=6)
  'LIMIT' => string ' var4' (length=5)

#1


2  

$cmd = 'SELECT var1 FROM var2 GROUP BY var3 LIMIT var4';

$array = array();
if(preg_match_all('/(SELECT|FROM|GROUP BY|LIMIT)\s+([^\s]+)/si', $cmd, $matches)) {
    $array = array_combine($matches[1], $matches[2]);
}

var_dump($array);

Result:

结果:

array
  'SELECT' => string 'var1' (length=4)
  'FROM' => string 'var2' (length=4)
  'GROUP BY' => string 'var3' (length=4)
  'LIMIT' => string 'var4' (length=4)

#2


3  

Have you considered using a SQL parser? You're bound to get wrong results with this regex-hack.

您是否考虑过使用SQL解析器?这个正则表达式攻击你一定会得到错误的结果。

Checkout:

查看:

  1. http://code.google.com/p/php-sql-parser
  2. http://code.google.com/p/php-sql-parser
  3. http://pear.php.net/package/SQL_Parser
  4. http://pear.php.net/package/SQL_Parser
  5. PHP MySQL SQL parser (INSERT and UPDATE)
  6. PHP MySQL SQL解析器(INSERT和UPDATE)

A demo with option 1:

带选项1的演示:

#!/usr/bin/env php
<?php

// http://php-sql-parser.googlecode.com/svn/trunk/php-sql-parser.php
require_once('php-sql-parser.php');

$cmd = 'SELECT var1 FROM var2 GROUP BY var3 LIMIT var4';
$parser = new PHPSQLParser($cmd);
$parse_tree = $parser->parsed;

echo 'SELECT -> ' . $parse_tree['SELECT'][0]['base_expr'] . "\n";
echo 'FROM   -> ' . $parse_tree['FROM'][0]['table'] . "\n";
echo 'GROUP  -> ' . $parse_tree['GROUP'][0]['base_expr'] . "\n";
echo 'LIMIT  -> ' . $parse_tree['LIMIT']['end'] . "\n";

?>

produces:

生产:

SELECT -> var1
FROM   -> var2
GROUP  -> var3
LIMIT  -> var4

#3


1  

Try

尝试

$matches = array();
preg_match ('~^SELECT (?P<SELECT>.+) FROM (?P<FROM>.+) GROUP BY (?P<GROUPBY>.+) LIMIT (?P<LIMIT>.+)~', $cmd, $matches);

Or is there any way to get delimiters?

或者有没有办法获得分隔符?

Regular expressions always starts with their delimiters (because its the other way round: The first character defines the delimiter)

正则表达式总是从它们的分隔符开始(因为它反过来:第一个字符定义分隔符)

echo $regEx[0];

#4


1  

a bit messy...

有点乱......

//echo file_get_contents('http://wp.me/pbZy8-1WM');
$cmd = 'SELECT var1 FROM var2 GROUP BY var3 LIMIT var4';
// split command
$pattern = 'SELECT|FROM|GROUP BY|LIMIT';
$p = preg_split('/('.$pattern.')/si', $cmd, null,PREG_SPLIT_DELIM_CAPTURE);
foreach($p as $i => $str)
if (strpos($pattern,$str) !== false)
{
    $rez[$str] = $p[$i+1];
} 
var_dump($rez);

gives:

得到:

  'SELECT' => string ' var1 ' (length=6)
  'FROM' => string ' var2 ' (length=6)
  'GROUP BY' => string ' var3 ' (length=6)
  'LIMIT' => string ' var4' (length=5)