폴더 읽어들여, 그 안의 있는 파일의 코드를
지정한 PHP 버전 문법에 맞게 자동변환되는 거....
에디드플러스에 이 기능 들어가면 좋을텐뎅..ㅎ
요며칠 PHP7.2 갖고 놀면서 떠오른 생각이네요.
댓글 23개
// 현재 폴더내의 php 파일중에서 $row[a] => $row['a'] 형태로 변환하는건데
// 테스트용으로만 쓰세요.
$dir = '.';
$scanned_dir = scandir($dir);
foreach ($scanned_dir as $filename) {
if (preg_match('/\.php$/', $filename)) {
if (filesize($filename) == 0) continue;
$handle = fopen($filename, 'r') or die('Unable to open file!');
$contents = fread($handle, filesize($filename));
fclose($handle);
$set_contents = preg_replace('/(\$\w+\[)(\w+)(\])/', "$1'$2'$3", $contents);
$handle = fopen($filename, 'w+') or die('Unable to open file!');
if (fwrite($handle, $set_contents) === FALSE) {
echo "Cannot write to file '$filename'";
exit;
}
fclose($handle);
}
}
// 테스트용으로만 쓰세요.
$dir = '.';
$scanned_dir = scandir($dir);
foreach ($scanned_dir as $filename) {
if (preg_match('/\.php$/', $filename)) {
if (filesize($filename) == 0) continue;
$handle = fopen($filename, 'r') or die('Unable to open file!');
$contents = fread($handle, filesize($filename));
fclose($handle);
$set_contents = preg_replace('/(\$\w+\[)(\w+)(\])/', "$1'$2'$3", $contents);
$handle = fopen($filename, 'w+') or die('Unable to open file!');
if (fwrite($handle, $set_contents) === FALSE) {
echo "Cannot write to file '$filename'";
exit;
}
fclose($handle);
}
}
@슈와이 슈와이 님, 테스트해보니 아주 잘 작동하네요.
좋은 소스 공유 감사합니다.
근데, 써보다 욕심난 게...
혹시,
서브폴더까지 검색해서
아래 형식들도 한번에 바꾸는 게 가능할까요?^^;
정규표현식 까막눈이다보니... ^^;;;
기왕 욕심 낸 거.........걍 닥 적어놓을테니, 껄끄러운 건 걍 사뿐히 패스해주세용.^^
변수명[$배열키]['문자열'] (예) $list[$i][mb_id] 또는 $list[$k][mb_id]
변수명[$배열키][$배열키]['문자열'] (예) $list[$i][$k][mb_id]
변수명['문자열']['문자열'] (예) $list[aaa][bbbb]
<? ---> <?php
<?php= ---> <?php echo공백
좋은 소스 공유 감사합니다.
근데, 써보다 욕심난 게...
혹시,
서브폴더까지 검색해서
아래 형식들도 한번에 바꾸는 게 가능할까요?^^;
정규표현식 까막눈이다보니... ^^;;;
기왕 욕심 낸 거.........걍 닥 적어놓을테니, 껄끄러운 건 걍 사뿐히 패스해주세용.^^
변수명[$배열키]['문자열'] (예) $list[$i][mb_id] 또는 $list[$k][mb_id]
변수명[$배열키][$배열키]['문자열'] (예) $list[$i][$k][mb_id]
변수명['문자열']['문자열'] (예) $list[aaa][bbbb]
<? ---> <?php
<?php= ---> <?php echo공백
<?php
// 파일 소스가 다 날아갈 수 있으니 실행 전에 반드시 소스 백업 후 실행.
// 테스트 후 사용할 것
// 원본: sir.kr 회원 슈와이 님
// 보완: Homzzang.com 신비
$dir = '.';
$scanned_dir = scandir($dir);
foreach ($scanned_dir as $filename) {
if (preg_match('/\.php$/', $filename)) {
if ($filename == 'array_converter') continue; // 이 파일은 제외
if (filesize($filename) == 0) continue;
$handle = fopen($filename, 'r') or die('Unable to open file!');
$contents = fread($handle, filesize($filename));
fclose($handle);
// 1. 현재 폴더 php 파일에서 $row['a'] => $row['a'] 형태로 변환
$set_contents1 = preg_replace('/(\$\w+\[)(\w+)(\])/', "$1'$2'$3", $contents);
// 2. 현재 폴더 php 파일에서 $row[$a]['b'] => $row[$a]['b'] 형태로 변환
$set_contents2 = preg_replace('/(\$\w+\[)(\$\w+)(\]\[)(\w+)(\])/', "$1$2$3'$4'$5", $set_contents1);
// 3. 현재 폴더 php 파일에서 $row['a']['b'] => $row['a']['b'] 형태로 변환
$set_contents3 = preg_replace('/(\$\w+\[)(\w+)(\]\[)(\w+)(\])/', "$1'$2'$3'$4'$5", $set_contents2);
// 4. 현재 폴더 php 파일에서 $row[$a][$b][c] => $row[$a][$b]['c'] 형태로 변환
$set_contents4 = preg_replace('/(\$\w+\[)(\$\w+)(\]\[)(\$\w+)(\]\[)(\w+)(\])/', "$1$2$3$4$5'$'6$7", $set_contents3);
// 5. 현재 폴더 php 파일에서 $row[$a][b][c] => $row[$a]['b']['c'] 형태로 변환
$set_contents5 = preg_replace('/(\$\w+\[)(\$\w+)(\]\[)(\w+)(\]\[)(\w+)(\])/', "$1$2$3'$4'$5'$6'$7", $set_contents4);
$handle = fopen($filename, 'w+') or die('Unable to open file!');
if (fwrite($handle, $set_contents3) === FALSE) {
echo "Cannot write to file '$filename'";
exit;
}
fclose($handle);
}
}
echo '전환 완료';
?>
https://sir.kr/qa/287280
// 파일 소스가 다 날아갈 수 있으니 실행 전에 반드시 소스 백업 후 실행.
// 테스트 후 사용할 것
// 원본: sir.kr 회원 슈와이 님
// 보완: Homzzang.com 신비
$dir = '.';
$scanned_dir = scandir($dir);
foreach ($scanned_dir as $filename) {
if (preg_match('/\.php$/', $filename)) {
if ($filename == 'array_converter') continue; // 이 파일은 제외
if (filesize($filename) == 0) continue;
$handle = fopen($filename, 'r') or die('Unable to open file!');
$contents = fread($handle, filesize($filename));
fclose($handle);
// 1. 현재 폴더 php 파일에서 $row['a'] => $row['a'] 형태로 변환
$set_contents1 = preg_replace('/(\$\w+\[)(\w+)(\])/', "$1'$2'$3", $contents);
// 2. 현재 폴더 php 파일에서 $row[$a]['b'] => $row[$a]['b'] 형태로 변환
$set_contents2 = preg_replace('/(\$\w+\[)(\$\w+)(\]\[)(\w+)(\])/', "$1$2$3'$4'$5", $set_contents1);
// 3. 현재 폴더 php 파일에서 $row['a']['b'] => $row['a']['b'] 형태로 변환
$set_contents3 = preg_replace('/(\$\w+\[)(\w+)(\]\[)(\w+)(\])/', "$1'$2'$3'$4'$5", $set_contents2);
// 4. 현재 폴더 php 파일에서 $row[$a][$b][c] => $row[$a][$b]['c'] 형태로 변환
$set_contents4 = preg_replace('/(\$\w+\[)(\$\w+)(\]\[)(\$\w+)(\]\[)(\w+)(\])/', "$1$2$3$4$5'$'6$7", $set_contents3);
// 5. 현재 폴더 php 파일에서 $row[$a][b][c] => $row[$a]['b']['c'] 형태로 변환
$set_contents5 = preg_replace('/(\$\w+\[)(\$\w+)(\]\[)(\w+)(\]\[)(\w+)(\])/', "$1$2$3'$4'$5'$6'$7", $set_contents4);
$handle = fopen($filename, 'w+') or die('Unable to open file!');
if (fwrite($handle, $set_contents3) === FALSE) {
echo "Cannot write to file '$filename'";
exit;
}
fclose($handle);
}
}
echo '전환 완료';
?>
https://sir.kr/qa/287280
/**
* 하위폴더의 php 파일을 검색해서 변환
* $변수명[문자열] => $변수명['문자열']
* <? => <?php
* <?= => <?php echo
* 변형된 파일명 출력
* 백업후 테스트용으로 쓰세요
*/
// read all files of subdirectories
function scan_dir($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
$files = array_merge($files, scan_dir($dir.'/'.basename($pattern), $flags));
return $files;
}
function add_single_quote($match) {
return preg_replace("/(\[)([^$\d']\w*)(\])/", "$1'$2'$3", $match[0]);
}
$scanned_dir = scan_dir('*.php');
$converted_files = array();
foreach ($scanned_dir as $filename) {
if (filesize($filename) == 0 || basename(__FILE__) == $filename) continue;
// read
$handle = fopen($filename, 'r') or die('Unable to open file!');
$contents = fread($handle, filesize($filename));
fclose($handle);
// convert
$set_contents = preg_replace_callback('/(?<=[\n= ])\$\S+\]/', 'add_single_quote', $contents);
$set_contents = preg_replace(array('/<\?(?!php)/', '/<\?php=/'), array('<?php', '<?php echo '), $set_contents);
if (strcmp($contents, $set_contents) == 0) continue;
// write
$converted_files[] = $filename;
$handle = fopen($filename, 'w+') or die('Unable to open file!');
if (fwrite($handle, $set_contents) === FALSE) {
echo "Cannot write to file '$filename'";
exit;
}
fclose($handle);
}
// print
$cnt = count($converted_files);
echo '#converted files ', $cnt, '개<br>';
if ($cnt)
echo implode('<br>', $converted_files);
?>
* 하위폴더의 php 파일을 검색해서 변환
* $변수명[문자열] => $변수명['문자열']
* <? => <?php
* <?= => <?php echo
* 변형된 파일명 출력
* 백업후 테스트용으로 쓰세요
*/
// read all files of subdirectories
function scan_dir($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
$files = array_merge($files, scan_dir($dir.'/'.basename($pattern), $flags));
return $files;
}
function add_single_quote($match) {
return preg_replace("/(\[)([^$\d']\w*)(\])/", "$1'$2'$3", $match[0]);
}
$scanned_dir = scan_dir('*.php');
$converted_files = array();
foreach ($scanned_dir as $filename) {
if (filesize($filename) == 0 || basename(__FILE__) == $filename) continue;
// read
$handle = fopen($filename, 'r') or die('Unable to open file!');
$contents = fread($handle, filesize($filename));
fclose($handle);
// convert
$set_contents = preg_replace_callback('/(?<=[\n= ])\$\S+\]/', 'add_single_quote', $contents);
$set_contents = preg_replace(array('/<\?(?!php)/', '/<\?php=/'), array('<?php', '<?php echo '), $set_contents);
if (strcmp($contents, $set_contents) == 0) continue;
// write
$converted_files[] = $filename;
$handle = fopen($filename, 'w+') or die('Unable to open file!');
if (fwrite($handle, $set_contents) === FALSE) {
echo "Cannot write to file '$filename'";
exit;
}
fclose($handle);
}
$cnt = count($converted_files);
echo '#converted files ', $cnt, '개<br>';
if ($cnt)
echo implode('<br>', $converted_files);
?>
게시글 목록
| 번호 | 제목 |
|---|---|
| 1717652 | |
| 1717651 | |
| 1717650 | |
| 1717648 | |
| 1717635 | |
| 1717629 | |
| 1717626 | |
| 1717625 | |
| 1717621 | |
| 1717619 | |
| 1717611 | |
| 1717610 | |
| 1717609 | |
| 1717607 | |
| 1717601 | |
| 1717598 | |
| 1717591 | |
| 1717590 | |
| 1717583 | |
| 1717575 | |
| 1717572 | |
| 1717568 | |
| 1717566 | |
| 1717549 | |
| 1717545 | |
| 1717533 | |
| 1717512 | |
| 1717511 | |
| 1717508 | |
| 1717495 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기