나는 삽입 HTML을 생성하기 위해 일부 videosites URL을 구문 분석하는 함수를 쓰고 있어요 :preg_match()에 전달하기 전에 strstr()을 사용하여 느슨하게 유효성을 검증해야합니까?
if (strstr($url, 'a.com')) {
$from = 'a';
} elseif (strstr($url, 'b.com')) {
$from = 'b';
} else {
return 'Wrong Video Url!';
}
if ($from == 'a') {
// use preg_match() to retrieve video id to generate embedding html
if (preg_match('#^http://a\.com/id_(\w*?)\.html$#', $url, $matches)) {
// return video embedding html
}
return 'Wrong a.com Video Url!';
}
if ($from == 'b') {
if (preg_match('#^http://b\.com/v_(\w*?)\.html$#', $url, $matches)) {
//return video embedding html
}
return 'Wrong b.com Video Url!';
}
이 같은 b.com
URL이있는 경우, 예를 들어, 어떤 상황에서 preg_match()
의 호출을 줄일 수있다 strstr()
를 사용하여 내 목적 : http://www.b.com/v_OTQ2MDE4MDg.html
, preg_match()
번으로 전화하지 않아도됩니다.
그러나 이런 종류의 연습이 좋은지 또는 더 좋은 방법이 있는지 확실하지 않습니다.
'regex'에 대한 도움말 : http://stackoverflow.com/questions/17900004/turn-plain-text-urls-into-active-links-using-php/17900021#17900021 –