[FastRoute] PHP 팁

2020. 1. 8. 01:56FastRoute (PHP)

1. PHP 비밀번호 검증 (mainController.php)

if (mb_strlen($password, 'utf-8') < 5 || mb_strlen($password, 'utf-8') > 20) {
	$res->isSuccess = false;
	$res->code = 500;
	$res->message = "비밀번호를 최소 5자리 최대 20자리 입력해주세요.";
	echo json_encode($res, JSON_NUMERIC_CHECK);
	return;
}

 

 

 

2. PHP 이메일 검증 (mainController.php)

if (!preg_match("/([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/", $email)) {
	$res->isSuccess = false;
	$res->code = 500;
	$res->message = "잘못된 이메일 형식입니다.";
	echo json_encode($res, JSON_NUMERIC_CHECK);
	return;
}

 

 

 

3. PHP foreach문을 이용하여 배열의 각 값에 대해 반복작업을 수행

foreach($param AS $value) {
	$st->execute([$value]);
} unset($value);

 

 

 

4. PHP 공백 제거 및 SQL LIKE 쿼리 적용

$value = preg_replace("/\s+/", "", $param);
function test($param) {
	$pdo = pdoSqlConnect();
	$query = "SELECT * FROM User WHERE id LIKE ?;";

	$st = $pdo->prepare($query);
	$st->execute(["%$param%"]);
	$st->setFetchMode(PDO::FETCH_ASSOC);
	$res = $st->fetchAll();

	$st=null;$pdo = null;

	return $res;
}

 

 

 

5. PHP의 특징을 이용하여 SQL 쿼리문을 분리하여 효율성을 높일 수 있음

$first = "SELECT * ";
$second = "FROM User ";
$third = "WHERE id = ?;";
$query = $first.$second.$third;
728x90

'FastRoute (PHP)' 카테고리의 다른 글

[FastRoute] PHP 이메일  (0) 2020.01.08
[FastRoute] PDO 사용법  (0) 2020.01.07
[FastRoute] PHP 트랜잭션  (0) 2020.01.07
[FastRoute] 로그인 토큰 인증 (2)  (0) 2020.01.07