PHPを使用していくつかの正規表現に苦労しています。
達成したいこと
- 特定の場所にあるすべてのファイルを繰り返し処理したい
- ファイルが sql ファイル (拡張子 .sql で識別される) である場合、それを開いて、正規表現を使用してすべての abc@xyz 一致を見つけたい
これまでに達成したこと
- すべてのディレクトリを通過する
- 正規表現とのマッチングを行いますが、@xyz 部分のみです
助けてほしいこと
- $matches 配列に @xyz の代わりに abc@xyz を格納するように正規表現を変更するにはどうすればよいですか?
コード
<?php
$path = realpath('.');
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
# go through each file/directory
foreach($objects as $name => $object){
#check if it is a sql file
if (strpos($name,'.sql') > 0) {
#open the file
$file = file_get_contents($name);
# check if the file could be succesfully read
if ($file) {
#if so we are looking for the @ sign identifying a db link
if (strpos($file,'@') > 0) {
# we found at least one @ sign, now go through the file again and again and again...
$at_pos=0;
while ($at_pos=strpos($file,'@',$at_pos+1)) {
echo "we got a db link in $name at position: $at_pos\n";
$result=preg_match("{\b\w*@\w*\b}",$file,$matches,PREG_OFFSET_CAPTURE,$at_pos);
print_r($matches);
}
}
} else {
echo "We could not open $name\n";
}
}
}
?>
サンプルの test2.sql ファイル
-- thsis is a file with a db_link
select * from abc@db_link;
but look we also got Select * from ddks@db_link2;