$string1 = 'google.com';
$string2 = 'http://www.google.com';
>. List. [ substr ] (https://www.php.net/manual/function.substr.php) 기능을 일부 반환되기를 구체화하십시오.
substr( $string_n, 0, 4 ) === "http"
substr( $string_n, 0, 7 ) === "http://"
또한 일반:
substr($string, 0, strlen($query)) === $query
['스트라포스 ()'] 사용하여 (https://www.php.net/manual/en/function.strpos.php):
if (strpos($string2, 'http') === 0) {
// It starts with 'http'
}
3 는 예증들을 너희에게베풀어진 ('='). 2 만 사용할 경우 제대로 작동하지 않습니다. 이 때문에 '스트라포스 () 에서 찾을 수 없는 경우' 거짓 '바늘' 반환되므로 짚단.
또한 '스테른시모프 ()' 및 '스테른카체mp ()' 함수은 완벽하겠어요 기능을 하는 이 상황:
if (strncmp($string_n, "http", 4) === 0)
일반적으로:
if (strncmp($string_n, $prefix, strlen($prefix)) === 0)
이점 '을 통해 substr ()', '그냥' () 는 외곽진입 에로남이네 스테른시모프 만들지 않고 해야 할 일에 임시 구체화하십시오.
Regex 간편하게 사용할 수 있습니다 (있다 '는' 에서 업데이트된 버전을 사용자 viriathus 레지 사용되지 않음)
if (preg_match('#^http#', $url) === 1) {
// Starts with http (case sensitive).
}
또는 스케쳐내 경우 검색 구분하지 않습니다.
if (preg_match('#^http#i', $url) === 1) {
// Starts with http (case insensitive).
}
if (preg_match('#^https?://#i', $url) === 1) {
// Starts with http:// or https:// (case insensitive).
}
>. 이 확장명은 노드전역 유지보수합니다 스레드할 당 캐시 컴파일됨 일반 >. 표현식에서는 (최대 4096). http://www.php.net/manual/en/intro.pcre.php
Http 나 https 기능을 사용하여 아래 작은 구체화하십시오 시작하는 경우 확인할 수 있습니다.
function has_prefix($string, $prefix) {
return substr($string, 0, strlen($prefix)) == $prefix;
}
$url = 'http://www.google.com';
echo 'the url ' . (has_prefix($url, 'http://') ? 'does' : 'does not') . ' start with http://';
echo 'the url ' . (has_prefix($url, 'https://') ? 'does' : 'does not') . ' start with https://';