사이트 내 전체검색
아래는 PHP에서 정규식을 사용하여 문자열의 앞부분이 "abc"로 시작하는 경우를 "DEF"로 바꾸는 예제입니다. preg_replace() 함수를 사용하여 이 작업을 수행할 수 있습니다:
하우코드
https://cmd.kr/preg/4 URL이 복사되었습니다.

본문

PHP에서 정규식을 사용하여 문자열의 앞부분이 "abc"로 시작하는 경우를 "DEF"로 바꾸는 예제
<?php
$original_string = "abcHello, world!";
$pattern = '/^abc/'; // 문자열의 시작이 "abc"인 경우를 찾습니다.
$replacement = 'DEF';
$new_string = preg_replace($pattern, $replacement, $original_string);
echo $new_string; // 출력: "DEFHello, world!"
?>
<?php
//2024-02-10
include_once('./_common.php');
$rst = sql_query('SHOW TABLES');
$old_str='g5zz_';
$new_str='g5cc_';
while($row = sql_fetch_array($rst)) {
$tablename=array_values($row)[0];// $row[0];  가 안될때
$pattern = "/^$old_str/"; // 문자열의 시작이 "abc"인 경우를 찾습니다.
$replacement = $new_str;
$new_tablename = preg_replace($pattern, $replacement, $tablename);
if ($new_tablename && $tablename != $new_tablename) {
echo "$tablename -> $new_tablename<BR>"; // 출력: "DEFHello, world!"
$sql="RENAME TABLE `{$tablename}` TO `{$new_tablename}`";
echo "$sql<BR>";
sql_query($sql);
}
}

댓글목록

등록된 댓글이 없습니다.

Search

Copyright © Cmd 명령어 3.144.127.232