0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

try catchを活用してURL判断する

0
Last updated at Posted at 2025-04-24

class InvalidURLException extends Exception {}

function checkURL($url) {
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new InvalidURLException("URL形式が正しくありません。");
}
return true;
}

try {
$inputURL = "https://example.com"; // 여기에 테스트할 URL 입력
checkURL($inputURL);
echo "✅ 有効なURLです: " . $inputURL;
} catch (InvalidURLException $e) {
echo "❌ エラー: " . $e->getMessage();
}

$inputURL = "https://example.com"; // ✅ 正常 URL
$inputURL = "ht@tp//example"; // ❌ 間違った URL
$inputURL = "www.example.com"; // ❌ http/https 抜き → エラー

filter_var($url, FILTER_VALIDATE_URL)はURLが正しいかどうかをはんだんしてくれます。もしURLが間違ったらInvalidURLExceptionエラーを投げます。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?