forked from gerkirill/dbStructSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbStruct.php
768 lines (746 loc) · 22.4 KB
/
dbStruct.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
<?
/**
TODO: make it work even without ';' delimiters or at least warn about that
TODO: better parse error reporting
TODO: accept empty datetime value and 0000-00-00 00:00:00 are equal, similar with date and time, also enum('0','1') [default 0], what's with floats?(float(10,2) NOT NULL default '0.00'); text,mediumtext,etc;
TODO: option to add database name with dot before the table names
TODO: add option "order does matter"
DONE: breaks table definition on commas and brackets, not newlines
DONE: handles `database`.`table` in CREATE TABLE string (but does not add database to result sql for a while - and if it
should? as same tables struct in 2 DBs compared is also a case)
DONE: handles double (and more) spaces in CREATE TABLE string
DONE: add filter option (fields: MODIFY, ADD, DROP, tables: CREATE, DROP)
DONE: make it work also with comments
DONE: move all options to $this->config
*/
/**
* The class provides ability to compare 2 database structure dumps and compile a set of sql statements to update
* one database to make it structure identical to another.
*
* @author Kirill Gerasimenko <ger.kirill@gmail.com>
*
* The input for the script could be taken from the phpMyAdmin structure dump, or provided by some custom code
* that uses 'SHOW CREATE TABLE' query to get database structure table by table.
* The output is either array of sql statements suitable for executions right from php or a string where the
* statements are placed each at new line and delimited with ';' - suitable for execution from phpMyAdmin SQL
* page.
* The resulting sql may contain queries that aim to:
* Create missing table (CREATE TABLE query)
* Delete table which should not longer exist (DROP TABLE query)
* Update, drop or add table field or index definition (ALTER TABLE query)
*
* Some features:
* - AUTO_INCREMENT value is ommited during the comparison and in resulting CREATE TABLE sql
* - fields with definitions like "(var)char (255) NOT NULL default ''" and "(var)char (255) NOT NULL" are treated
* as equal, the same for (big|tiny)int NOT NULL default 0;
* - IF NOT EXISTS is automatically added to the resulting sql CREATE TABLE statement
* - fields updating queries always come before key modification ones for each table
* Not implemented:
* - The class even does not try to insert or re-order fields in the same order as in the original table.
* Does order matter?
* IMPORTANT!!! Class will not handle a case when the field was renamed. It will generate 2 queries - one to drop
* the column with the old name and one to create column with the new name, so if there is a data in the dropped
* column, it will be lost.
* Usage example:
$updater = new dbStructUpdater();
$res = $updater->getUpdates($struct1, $struct2);
-----
$res == array (
[0]=>"ALTER TABLE `b` MODIFY `name` varchar(255) NOT NULL",
...
)
*/
class dbStructUpdater
{
var $sourceStruct = '';//structure dump of the reference database
var $destStruct = '';//structure dump of database to update
var $config = array();//updater configuration
/**
* Constructor
* @access public
*/
function dbStructUpdater()
{
$this->init();
}
function init()
{
//table operations: create, drop; field operations: add, remove, modify
$this->config['updateTypes'] = 'create, drop, add, remove, modify';
//ignores default part in cases like (var)char NOT NULL default '' upon the comparison
$this->config['varcharDefaultIgnore'] = true;
//the same for int NOT NULL default 0
$this->config['intDefaultIgnore'] = true;
//ignores table autoincrement field value, also remove AUTO_INCREMENT value from the create query if exists
$this->config['ignoreIncrement'] = true;
//add 'IF NOT EXIST' to each CREATE TABLE query
$this->config['forceIfNotExists'] = true;
//remove 'IF NOT EXIST' if already exists CREATE TABLE dump
$this->config['ingoreIfNotExists'] = false;
}
/**
* merges current updater config with the given one
* @param assoc_array $config new configuration values
*/
function setConfig($config=array())
{
if (is_array($config))
{
$this->config = array_merge($this->config, $config);
}
}
/**
* Returns array of update SQL with default options, $source, $dest - database structures
* @access public
* @param string $source structure dump of database to update
* @param string $dest structure dump of the reference database
* @param bool $asString if true - result will be a string, otherwise - array
* @return array|string update sql statements - in array or string (separated with ';')
*/
function getUpdates($source, $dest, $asString=false)
{
$result = $asString?'':array();
$compRes = $this->compare($source, $dest);
if (empty($compRes))
{
return $result;
}
$compRes = $this->filterDiffs($compRes);
if (empty($compRes))
{
return $result;
}
$result = $this->getDiffSql($compRes);
if ($asString)
{
$result = implode(";\r\n", $result).';';
}
return $result;
}
/**
* Filters comparison result and lefts only sync actions allowed by 'updateTypes' option
*/
function filterDiffs($compRes)
{
$result = array();
if (is_array($this->config['updateTypes']))
{
$updateActions = $this->config['updateTypes'];
}
else
{
$updateActions = array_map('trim', explode(',', $this->config['updateTypes']));
}
$allowedActions = array('create', 'drop', 'add', 'remove', 'modify');
$updateActions = array_intersect($updateActions, $allowedActions);
foreach($compRes as $table=>$info)
{
if ($info['sourceOrphan'])
{
if (in_array('create', $updateActions))
{
$result[$table] = $info;
}
}
elseif ($info['destOrphan'])
{
if (in_array('drop', $updateActions))
{
$result[$table] = $info;
}
}
elseif($info['differs'])
{
$resultInfo = $info;
unset($resultInfo['differs']);
foreach ($info['differs'] as $diff)
{
if (empty($diff['dest']) && in_array('add', $updateActions))
{
$resultInfo['differs'][] = $diff;
}
elseif (empty($diff['source']) && in_array('remove', $updateActions))
{
$resultInfo['differs'][] = $diff;
}
elseif(in_array('modify', $updateActions))
{
$resultInfo['differs'][] = $diff;
}
}
if (!empty($resultInfo['differs']))
{
$result[$table] = $resultInfo;
}
}
}
return $result;
}
/**
* Gets structured general info about the databases diff :
* array(sourceOrphans=>array(...), destOrphans=>array(...), different=>array(...))
*/
function getDiffInfo($compRes)
{
if (!is_array($compRes))
{
return false;
}
$result = array('sourceOrphans'=>array(), 'destOrphans'=>array(), 'different'=>array());
foreach($compRes as $table=>$info)
{
if ($info['sourceOrphan'])
{
$result['sourceOrphans'][] = $table;
}
elseif ($info['destOrphan'])
{
$result['destOrphans'][] = $table;
}
else
{
$result['different'][] = $table;
}
}
return $result;
}
/**
* Makes comparison of the given database structures, support some options
* @access private
* @param string $source and $dest are strings - database tables structures
* @return array
* - table (array)
* - destOrphan (boolean)
* - sourceOrphan (boolean)
* - differs (array) OR (boolean) false if no diffs
* - [0](array)
* - source (string) structure definition line in the out-of-date table
* - dest (string) structure definition line in the reference table
* - [1](array) ...
*/
function compare($source, $dest)
{
$this->sourceStruct = $source;
$this->destStruct = $dest;
$result = array();
$destTabNames = $this->getTableList($this->destStruct);
$sourceTabNames = $this->getTableList($this->sourceStruct);
$common = array_intersect($destTabNames, $sourceTabNames);
$destOrphans = array_diff($destTabNames, $common);
$sourceOrphans = array_diff($sourceTabNames, $common);
$all = array_unique(array_merge($destTabNames, $sourceTabNames));
sort($all);
foreach ($all as $tab)
{
$info = array('destOrphan'=>false, 'sourceOrphan'=>false, 'differs'=>false);
if(in_array($tab, $destOrphans))
{
$info['destOrphan'] = true;
}
elseif (in_array($tab, $sourceOrphans))
{
$info['sourceOrphan'] = true;
}
else
{
$destSql = $this->getTabSql($this->destStruct, $tab, true);
$sourceSql = $this->getTabSql($this->sourceStruct, $tab, true);
$diffs = $this->compareSql($sourceSql, $destSql);
if ($diffs===false)
{
trigger_error('[WARNING] error parsing definition of table "'.$tab.'" - skipped');
continue;
}
elseif (!empty($diffs))//not empty array
{
$info['differs'] = $diffs;
}
else continue;//empty array
}
$result[$tab] = $info;
}
return $result;
}
/**
* Retrieves list of table names from the database structure dump
* @access private
* @param string $struct database structure listing
*/
function getTableList($struct)
{
$result = array();
if (preg_match_all('/CREATE(?:\s*TEMPORARY)?\s*TABLE\s*(?:IF NOT EXISTS\s*)?(?:`?(\w+)`?\.)?`?(\w+)`?/i', $struct, $m))
{
foreach($m[2] as $match)//m[1] is a database name if any
{
$result[] = $match;
}
}
return $result;
}
/**
* Retrieves table structure definition from the database structure dump
* @access private
* @param string $struct database structure listing
* @param string $tab table name
* @param bool $removeDatabase - either to remove database name in "CREATE TABLE database.tab"-like declarations
* @return string table structure definition
*/
function getTabSql($struct, $tab, $removeDatabase=true)
{
$result = '';
/* create table should be single line in this case*/
//1 - part before database, 2-database name, 3 - part after database
if (preg_match('/(CREATE(?:\s*TEMPORARY)?\s*TABLE\s*(?:IF NOT EXISTS\s*)?)(?:`?(\w+)`?\.)?(`?('.$tab.')`?(\W|$))/i', $struct, $m, PREG_OFFSET_CAPTURE))
{
$tableDef = $m[0][0];
$start = $m[0][1];
$database = $m[2][0];
$offset = $start+strlen($m[0][0]);
$end = $this->getDelimPos($struct, $offset);
if ($end === false)
{
$result = substr($struct, $start);
}
else
{
$result = substr($struct, $start, $end-$start);//already without ';'
}
}
$result = trim($result);
if ($database && $removeDatabase)
{
$result = str_replace($tableDef, $m[1][0].$m[3][0], $result);
}
return $result;
}
/**
* Splits table sql into indexed array
*
*/
function splitTabSql($sql)
{
$result = array();
//find opening bracket, get the prefix along with it
$openBracketPos = $this->getDelimPos($sql, 0, '(');
if ($openBracketPos===false)
{
trigger_error('[WARNING] can not find opening bracket in table definition');
return false;
}
$prefix = substr($sql, 0, $openBracketPos+1);//prefix can not be empty, so do not check it, just trim
$result[] = trim($prefix);
$body = substr($sql, strlen($prefix));//fields, indexes and part after closing bracket
//split by commas, get part by part
while(($commaPos = $this->getDelimPos($body, 0, ',', true))!==false)
{
$part = trim(substr($body, 0, $commaPos+1));//read another part and shorten $body
if ($part)
{
$result[] = $part;
}
$body = substr($body, $commaPos+1);
}
//here we have last field (or index) definition + part after closing bracket (ENGINE, ect)
$closeBracketPos = $this->getDelimRpos($body, 0, ')');
if ($closeBracketPos===false)
{
trigger_error('[WARNING] can not find closing bracket in table definition');
return false;
}
//get last field / index definition before closing bracket
$part = substr($body, 0, $closeBracketPos);
$result[] = trim($part);
//get the suffix part along with the closing bracket
$suffix = substr($body, $closeBracketPos);
$suffix = trim($suffix);
if ($suffix)
{
$result[] = $suffix;
}
return $result;
}
/**
* returns array of fields or keys definitions that differs in the given tables structure
* @access private
* @param sring $sourceSql table structure
* @param sring $destSql right table structure
* supports some $options
* @return array
* - [0]
* - source (string) out-of-date table field definition
* - dest (string) reference table field definition
* - [1]...
*/
function compareSql($sourceSql, $destSql)//$sourceSql, $destSql
{
$result = array();
//split with comma delimiter, not line breaks
$sourceParts = $this->splitTabSql($sourceSql);
if ($sourceParts===false)//error parsing sql
{
trigger_error('[WARNING] error parsing source sql');
return false;
}
$destParts = $this->splitTabSql($destSql);
if ($destParts===false)
{
trigger_error('[WARNING] error parsing destination sql');
return false;
}
$sourcePartsIndexed = array();
$destPartsIndexed = array();
foreach($sourceParts as $line)
{
$lineInfo = $this->processLine($line);
if (!$lineInfo) continue;
$sourcePartsIndexed[$lineInfo['key']] = $lineInfo['line'];
}
foreach($destParts as $line)
{
$lineInfo = $this->processLine($line);
if (!$lineInfo) continue;
$destPartsIndexed[$lineInfo['key']] = $lineInfo['line'];
}
$sourceKeys = array_keys($sourcePartsIndexed);
$destKeys = array_keys($destPartsIndexed);
$all = array_unique(array_merge($sourceKeys, $destKeys));
sort($all);//fields first, then indexes - because fields are prefixed with '!'
foreach ($all as $key)
{
$info = array('source'=>'', 'dest'=>'');
$inSource= in_array($key, $sourceKeys);
$inDest= in_array($key, $destKeys);
$sourceOrphan = $inSource && !$inDest;
$destOrphan = $inDest && !$inSource;
$different = $inSource && $inDest &&
strcasecmp($this->normalizeString($destPartsIndexed[$key]), $this->normalizeString($sourcePartsIndexed[$key]));
if ($sourceOrphan)
{
$info['source'] = $sourcePartsIndexed[$key];
}
elseif ($destOrphan)
{
$info['dest'] = $destPartsIndexed[$key];
}
elseif ($different)
{
$info['source'] = $sourcePartsIndexed[$key];
$info['dest'] = $destPartsIndexed[$key];
}
else continue;
$result[] = $info;
}
return $result;
}
/**
* Transforms table structure defnition line into key=>value pair where the key is a string that uniquely
* defines field or key desribed
* @access private
* @param string $line field definition string
* @return array array with single key=>value pair as described in the description
* implements some options
*/
function processLine($line)
{
$options = $this->config;
$result = array('key'=>'', 'line'=>'');
$line = rtrim(trim($line), ',');
if (preg_match('/^(CREATE\s+TABLE)|(\) ENGINE=)/i', $line))//first or last table definition line
{
return false;
}
//if (preg_match('/^(PRIMARY KEY)|(((UNIQUE )|(FULLTEXT ))?KEY `?\w+`?)/i', $line, $m))//key definition
if (preg_match('/^(PRIMARY\s+KEY)|(((UNIQUE\s+)|(FULLTEXT\s+))?KEY\s+`?\w+`?)/i', $line, $m))//key definition
{
$key = $m[0];
}
elseif (preg_match('/^`?\w+`?/i', $line, $m))//field definition
{
$key = '!'.$m[0];//to make sure fields will be synchronised before the keys
}
else
{
return false;//line has no valuable info (empty or comment)
}
//$key = str_replace('`', '', $key);
if (!empty($options['varcharDefaultIgnore']))
{
$line = preg_replace("/(var)?char\(([0-9]+)\)\s+NOT\s+NULL\s+default\s+''/i", '$1char($2) NOT NULL', $line);
}
if (!empty($options['intDefaultIgnore']))
{
$line = preg_replace("/((?:big)|(?:tiny))?int\(([0-9]+)\)\s+NOT\s+NULL\s+default\s+'0'/i", '$1int($2) NOT NULL', $line);
}
if (!empty($options['ignoreIncrement']))
{
$line = preg_replace("/ AUTO_INCREMENT=[0-9]+/i", '', $line);
}
$result['key'] = $this->normalizeString($key);
$result['line']= $line;
return $result;
}
/**
* Takes an output of compare() method to generate the set of sql needed to update source table to make it
* look as a destination one
* @access private
* @param array $diff compare() method output
* @return array list of sql statements
* supports query generation options
*/
function getDiffSql($diff)//maybe add option to ommit or force 'IF NOT EXISTS', skip autoincrement
{
$options = $this->config;
$sqls = array();
if (!is_array($diff) || empty($diff))
{
return $sqls;
}
foreach($diff as $tab=>$info)
{
if ($info['sourceOrphan'])//delete it
{
$sqls[] = 'DROP TABLE `'.$tab.'`';
}
elseif ($info['destOrphan'])//create destination table in source
{
$database = '';
$destSql = $this->getTabSql($this->destStruct, $tab, $database);
if (!empty($options['ignoreIncrement']))
{
$destSql = preg_replace("/\s*AUTO_INCREMENT=[0-9]+/i", '', $destSql);
}
if (!empty($options['ingoreIfNotExists']))
{
$destSql = preg_replace("/IF NOT EXISTS\s*/i", '', $destSql);
}
if (!empty($options['forceIfNotExists']))
{
$destSql = preg_replace('/(CREATE(?:\s*TEMPORARY)?\s*TABLE\s*)(?:IF\sNOT\sEXISTS\s*)?(`?\w+`?)/i', '$1IF NOT EXISTS $2', $destSql);
}
$sqls[] = $destSql;
}
else
{
foreach($info['differs'] as $finfo)
{
$inDest = !empty($finfo['dest']);
$inSource = !empty($finfo['source']);
if ($inSource && !$inDest)
{
$sql = $finfo['source'];
$action = 'drop';
}
elseif ($inDest && !$inSource)
{
$sql = $finfo['dest'];
$action = 'add';
}
else
{
$sql = $finfo['dest'];
$action = 'modify';
}
$sql = $this->getActionSql($action, $tab, $sql);
$sqls[] = $sql;
}
}
}
return $sqls;
}
/**
* Compiles update sql
* @access private
* @param string $action - 'drop', 'add' or 'modify'
* @param string $tab table name
* @param string $sql definition of the element to change
* @return string update sql
*/
function getActionSql($action, $tab, $sql)
{
$result = 'ALTER TABLE `'.$tab.'` ';
$action = strtolower($action);
$keyField = '`?\w`?(?:\(\d+\))?';//matches `name`(10)
$keyFieldList = '(?:'.$keyField.'(?:,\s?)?)+';//matches `name`(10),`desc`(255)
if (preg_match('/((?:PRIMARY )|(?:UNIQUE )|(?:FULLTEXT ))?KEY `?(\w+)?`?\s(\('.$keyFieldList.'\))/i', $sql, $m))
{ //key and index operations
$type = strtolower(trim($m[1]));
$name = trim($m[2]);
$fields = trim($m[3]);
switch($action)
{
case 'drop':
if ($type=='primary')
{
$result.= 'DROP PRIMARY KEY';
}
else
{
$result.= 'DROP INDEX `'.$name.'`';
}
break;
case 'add':
if ($type=='primary')
{
$result.= 'ADD PRIMARY KEY '.$fields;
}
elseif ($type=='')
{
$result.= 'ADD INDEX `'.$name.'` '.$fields;
}
else
{
$result .='ADD '.strtoupper($type).' `'.$name.'` '.$fields;//fulltext or unique
}
break;
case 'modify':
if ($type=='primary')
{
$result.='DROP PRIMARY KEY, ADD PRIMARY KEY '.$fields;
}
elseif ($type=='')
{
$result.='DROP INDEX `'.$name.'`, ADD INDEX `'.$name.'` '.$fields;
}
else
{
$result.='DROP INDEX `'.$name.'`, ADD '.strtoupper($type).' `'.$name.'` '.$fields;//fulltext or unique
}
break;
}
}
else //fields operations
{
$sql = rtrim(trim($sql), ',');
$result.= strtoupper($action);
if ($action=='drop')
{
$spacePos = strpos($sql, ' ');
$result.= ' '.substr($sql, 0, $spacePos);
}
else
{
$result.= ' '.$sql;
}
}
return $result;
}
/**
* Searches for the position of the next delimiter which is not inside string literal like 'this ; ' or
* like "this ; ".
*
* Handles escaped \" and \'. Also handles sql comments.
* Actualy it is regex-based Finit State Machine (FSN)
*/
function getDelimPos($string, $offset=0, $delim=';', $skipInBrackets=false)
{
$stack = array();
$rbs = '\\\\'; //reg - escaped backslash
$regPrefix = "(?<!$rbs)(?:$rbs{2})*";
$reg = $regPrefix.'("|\')|(/\\*)|(\\*/)|(-- )|(\r\n|\r|\n)|';
if ($skipInBrackets)
{
$reg.='(\(|\))|';
}
else
{
$reg.='()';
}
$reg .= '('.preg_quote($delim).')';
while (preg_match('%'.$reg.'%', $string, $m, PREG_OFFSET_CAPTURE, $offset))
{
$offset = $m[0][1]+strlen($m[0][0]);
if (end($stack)=='/*')
{
if (!empty($m[3][0]))
{
array_pop($stack);
}
continue;//here we could also simplify regexp
}
if (end($stack)=='-- ')
{
if (!empty($m[5][0]))
{
array_pop($stack);
}
continue;//here we could also simplify regexp
}
if (!empty($m[7][0]))// ';' found
{
if (empty($stack))
{
return $m[7][1];
}
else
{
//var_dump($stack, substr($string, $offset-strlen($m[0][0])));
}
}
if (!empty($m[6][0]))// '(' or ')' found
{
if (empty($stack) && $m[6][0]=='(')
{
array_push($stack, $m[6][0]);
}
elseif($m[6][0]==')' && end($stack)=='(')
{
array_pop($stack);
}
}
elseif (!empty($m[1][0]))// ' or " found
{
if (end($stack)==$m[1][0])
{
array_pop($stack);
}
else
{
array_push($stack, $m[1][0]);
}
}
elseif (!empty($m[2][0])) // opening comment / *
{
array_push($stack, $m[2][0]);
}
elseif (!empty($m[4][0])) // opening comment --
{
array_push($stack, $m[4][0]);
}
}
return false;
}
/**
* works the same as getDelimPos except returns position of the first occurence of the delimiter starting from
* the end of the string
*/
function getDelimRpos($string, $offset=0, $delim=';', $skipInBrackets=false)
{
$pos = $this->getDelimPos($string, $offset, $delim, $skipInBrackets);
if ($pos===false)
{
return false;
}
do
{
$newPos=$this->getDelimPos($string, $pos+1, $delim, $skipInBrackets);
if ($newPos !== false)
{
$pos = $newPos;
}
}
while($newPos!==false);
return $pos;
}
/**
* Converts string to lowercase and replaces repeated spaces with the single one -
* to be used for the comparison purposes only
* @param string $str string to normaize
*/
function normalizeString($str)
{
$str = strtolower($str);
$str = preg_replace('/\s+/', ' ', $str);
return $str;
}
}