2010-08-17 13 views
5

Dizenin başlangıcını 'http: //' ve '/' ve sonunu içerdiğinden emin olmanızı sağlayan normal ifadeye sahip olmak istiyorum. Ben bu iki ifade aşağıdaki gibi birlikte koyabilirsiniz düşüncepreg_match: başlangıç ​​ve sonunun bir şey içerdiğinden emin olun

if(!preg_match("/(^http:\/\//", $site_http)) 
{ 
$error = true; 
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start."/>'; 
} 
elseif (!preg_match("/\/$/", $site_http)) 
{ 
$error = true; 
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link has ended with a /."/>'; 
} 

ama

Bu benim ile geldi daha uzun versiyonu, ama alışkanlık iş,

if(!preg_match("/(^http:\/\/)&(\/$)/", $site_http)) 
{ 
$error = true; 
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start and a/at the end."/>'; 
} 

birden ifadeleri ben birleştirmeye çalışın yanlış olmalı! Herhangi bir fikir?

sayesinde Lau ön

+0

bir dize örneği verebilir:

<?php foreach (array("http://site.com.invalid/", "http://") as $site_http) { echo "$site_http - "; if (preg_match('/^http:\/\/.*\/$/', $site_http)) { echo "match\n"; } else { echo "no match\n"; } } ?> 

aşağıdaki çıktıyı üretir? – Codex73

cevap

9
if(preg_match('/^http:\/\/.*\/$/', $site_http)) 
{ 
    ... 
} 

^http:\/\/ kuvvetler http://, \/$ ucunda bir çizgi zorlar ve .* şeyi (ve muhtemelen hiçbir şey) arasındaki verir. Örneğin

:

http://site.com.invalid/ - match 
http:// - no match
+0

patlama, beni ona dövdün. Yeni bir cevap dediğinde yazıyordu. :) –

+0

Çok teşekkür ederim gbacon! efsane! :-)) – laukok

+0

@lauthiamkok Rica ederim! Yardım ettiğine sevindim. –

İlgili konular