2016-04-12 28 views
17

Come possiamo utilizzare PHP per identificare gli URL in una stringa e archiviarli in una matrice?Estrai URL da una stringa utilizzando PHP

Questa è una stringa di esempio.

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

non riesco a utilizzare la funzione di explode perché, se l'URL contiene una virgola, non ci vorrà dare risultati corretti.

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

risposta

36

REGEX è la risposta per il vostro problema. Prendendo la risposta del manipolatore oggetto .. tutto ciò che manca è da escludere "virgole", in modo da poter provare questo codice che li esclude e dà 3 URL separato è come output:

$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>"; 

e l'uscita è

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

Forse dovresti renderlo insensibile alle maiuscole aggiungendo il modificatore 'i'. vale a dire. '... # i'' – MrWhite

+0

Solo una nota, alcuni URL usano le virgole nelle loro stringhe di query – relipse

+0

@aampudia: Ottimo approccio. Ma c'è un modo semplice per trovare gli URL anche senza protocollo? Ad esempio: "Il testo da filtrare va qui. Www.google.de, www.youtube.com". – Marco

3

Si può provare Regex qui:

$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>"; 

Questo ha pronunciato la seguente uscita:

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

dovrebbe avere 3 risultati nell'array di output. non 2. 'http: // google.com',' https: //www.youtube.com/watch? v = K_m7NEDMrV0' e 'https: // instagram.com/hellow /' –

2

provare questo

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

o

$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]); 
} 

lo farà funziona

+0

No, continua a dare 2 risultati. ci sono 3 URL ma solo 2 vengono restituiti. Puoi vedere? '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 potrebbe essere questo che ti aiuterà – khan

+0

a fornire un esempio con quell'espressione regolare? –

4

riprova da usare sotto regex

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

Spero che questo funzionerà per voi

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, dà ancora solo 2 URL. dovrebbe dare 3 URL come risultato. –

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>";