2016-04-12 11 views
17

PHPを使用して文字列内のURLを識別し、配列に格納するにはどうすればよいですか?PHPを使用して文字列からURLを抽出する

これはサンプルの文字列です。 URLにカンマが含まれている場合、それは正しい結果を与える文句を言わない、ので、私はexplode機能を使用することはできません

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

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

答えて

36

REGEXは、あなたの問題の答えです。オブジェクトマニピュレータの回答を撮影...それが欠けているすべてはあなたがそれらを除外し、出力として3つの分離URL年代を与えるこのコードを試すことができますので、「カンマ」を除外することです:

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

と出力が

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

「i」修飾子を追加することで、大文字と小文字を区別しないようにしたいかもしれません。すなわち、 '...#i'' – MrWhite

+0

URLの中には、クエリ文字列にカンマを使用するものがあります。 – relipse

+0

@aampudia:非常に良いアプローチです。しかし、プロトコルなしでURLを見つける簡単な方法はありますか? 「あなたがフィルタリングしたいテキストはwww.google.de、www.youtube.com」になります。 – Marco

3

あなたはここに正規表現を試すことができます。

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

出力配列に3つの結果が含まれている必要があります。 2.「http:// google.com」、「https://www.youtube.com/watch?v = K_m7NEDMrV0'、「https:// instagram.com/hellow /」 –

2

この

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

または

を試してみてください。

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

これは、次のような出力を提供します

$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

まだ2結果。 3つのURLがありますが、2つだけが返されます。あなたは見えますか? '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これはあなたの助けを借りて – khan

+0

その正規表現の例を提供することができますか? –

4

を働く正規表現

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

の下に使用してみてくださいことを願っています、これはあなたのために動作します

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

いいえ、まだ2つしかURLを与えていません。結果として3つのURLを与える必要があります。 –

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>"; 
関連する問題