2016-04-12 14 views
17

URL'leri bir dizede tanımlamak ve bir dizide saklamak için PHP'yi nasıl kullanabiliriz?URL'leri PHP kullanarak bir dizeden ayıkla

Bu örnek bir dizedir. URL virgül içeriyorsa, bu alışkanlık doğru sonuçlar verir, çünkü

$text = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/"; 

Ben explode işlevini kullanamazsınız.

print_r (explode(" ",$text)); 

cevap

36

REGEX için çalışacak regex altında

$regex = '/https?\:\/\/[^\",]+/i'; 
preg_match_all($regex, $string, $matches); 
echo "<pre>"; 
print_r($matches[0]); 

Umut kullanmayı deneyin çalışır senin sorunun cevabıdır.

$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/"; 

preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match); 

echo "<pre>"; 
print_r($match[0]); 
echo "</pre>"; 

ve çıkış

Array 
(
    [0] => http://google.com 
    [1] => https://www.youtube.com/watch?v=K_m7NEDMrV0 
    [2] => https://instagram.com/hellow/ 
) 
geçerli: Nesne Kolunun Cevap alarak .. Tek eksik bütün bunları dışlar ve 3 ayrı URL çıktı olarak var veren bu kodu denemek böylece, "virgül" dışlamak için Çıkış dizisinde 3 sonuç olması gerekir.
+2

Belki de 'i' değiştiricisini ekleyerek büyük küçük harf duyarlı hale getirmek isteyebilirsiniz. yani. '... # i ' – MrWhite

+0

Sadece bir not, bazı URL'ler kendi sorgu dizelerinde virgül kullanıyor – relipse

+0

@aampudia: Çok iyi bir yaklaşım. Ama protokolleri olmayan urls bulmak için basit bir yolu var mı? Gibi: "Filtrelemek istediğiniz metin burada. Www.google.de, www.youtube.com". – Marco

3

Burada Regex deneyebilirsiniz:

Array 
(
    [0] => http://google.com 
    [1] => https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/ 
) 
+4

. değil 2. 'http: // google.com',' https: //www.youtube.com/watch? v = K_m7NEDMrV0' ve 'https: // instagram.com/hellow /' –

2

bu

function getUrls($string) 
{ 
$regex = '/https?\:\/\/[^\" ]+/i'; 
preg_match_all($regex, $string, $matches); 
return ($matches[0]); 
} 
$urls = getUrls($string); 
print_r($urls); 

veya

deneyin:

$string = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/"; 

preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $string, $match); 

echo "<pre>"; 
print_r($match[0]); 
echo "</pre>"; 

Bu aşağıdaki çıktıyı verir o-ecek

$str = '<a href="http://foobar.com"> | Hello world Im a http://google.fr |  Did you mean:http://google.fr/index.php?id=1&b=6#2310'; 
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;[email protected]]+)[^\w#$&+,\/:;[email protected]]*?`i'; 
if (preg_match_all($pattern,$str,$matches)) 
{ 
print_r($matches[1]); 
} 

+0

Hayır, hala veren 2 Sonuçlar. 3 tane URL var ama sadece 2 tane geri döndü. Görebiliyor musun? Array ([0] => http://google.com, [1] => https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/) ' –

+0

http://stackoverflow.com/questions/4390556/extract-url-from-string bu yardımcı olabilir – khan

+0

bu regex ile bir örnek verebilir misiniz? –

2
$urlstring = "The text you want to filter goes here. http://google.com, https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/"; 

preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $urlstring , $result); 

print_r($result[0]); 
+0

no, hala yalnızca 2 URL veriyor. Sonuç olarak 3 URL vermelidir. –

2
$string = "The text you want to filter goes here. http://google.com, 
https://www.youtube.com/watch?v=K_m7NEDMrV0,https://instagram.com/hellow/"; 

preg_match_all('#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', 
$string, $match); 

echo "<pre>"; $arr = explode(",", $match[0][1]); 
print_r($match[0][0]); print_r($arr); echo "</pre>"; 
İlgili konular