여기에 "https://를 문자열에 추가하는 함수를 설정했습니다. 다음 값을 전달했다고 가정해 보겠습니다. -
example.com
그리고 우리가 원하는 출력은 "https://", 즉 실제 링크입니다 -
https://example.com
이를 위해 dot(.) 표기법과 preg_match()와 조건부 일치를 사용할 수 있습니다.
예시
<!DOCTYPE html>
<body>
<?php
function addingTheHTTPValue($stringValue) {
if (!preg_match("~^(?:f|ht)tps?://~i", $stringValue)) {
$stringValue = "https://" . $stringValue;
}
return $stringValue;
}
echo addingTheHTTPValue("example.com");
echo "<br>";
echo addingTheHTTPValue("https://example.com");
?>
</body>
</html> 출력
https://example.com https://example.com