php - Extract all MP3 and OGG Links from String with preg_match_all -
i trying create regular expressions extract mp3/ogg links example word could't! example word i'm trying extract mp3/ogg files it:
this example word http://domain.com/sample.mp3 , second file https://www.mydomain.com/sample2.ogg. link third file <a href="http://seconddomain.com/files/music.mp3" target="_blank">download</a>
and php part:
$word = "this example word http://domain.com/sample.mp3 , second file https://www.mydomain.com/sample2.ogg. link third file <a href="http://seconddomain.com/files/music.mp3" target="_blank">download</a>"; $pattern = '/href=\"(.*?)\".mp3/'; preg_match_all($pattern,$word,$matches); print_r($matches);
i tried too:
$pattern = '/href="([^"]\.mp3|ogg)"/'; $pattern = '/([-a-z0-9_\/:.]+\.(mp3|ogg))/i';
so need fix code , extract mp3/ogg links example word.
thank guys.
to retrieve links, can use:
((https?:\/\/)?(\w+?\.)+?(\w+?\/)+\w+?.(mp3|ogg))
demo.
((https?:\/\/)?
optional http://
or https://
(\w+?\.)+?
matches domain groups
(\w+?\/)+
matches final domain group , forward slash
\w+?.(mp3|ogg))
matches filename ending in .mp3
or .ogg
.
in string provided there several unescaped quotation marks, when corrected , regex added in:
$word = "this example word http://domain.com/sample.mp3 , second file https://www.mydomain.com/sample2.ogg. link third file <a href=\"http://seconddomain.com/files/music.mp3\" target=\"_blank\">download</a>"; $pattern = '/((https?:\/\/)?(\w+?\.)+?(\w+?\/)+\w+?.(mp3|ogg))/im'; preg_match_all($pattern,$word,$matches); var_dump($matches[0]);
produces following output:
array (size=3) 0 => string 'http://domain.com/sample.mp3' (length=28) 1 => string 'https://www.mydomain.com/sample2.ogg' (length=36) 2 => string 'http://seconddomain.com/files/music.mp3' (length=39)
Comments
Post a Comment