0

I need your help I want to extract all m3u8 links From Text

$string = "http://exemple.com/file/01.mp4 ,http://exemple.com/file/02.m3u8 , http://exemple.com/file/01.mp3 ,http://exemple.com/file/05.m3u8 ,http://exemple.com/file/02.mp4" ;

Thank you in advance

  • Does this answer your question? [Regular expression - starting and ending with a character string](https://stackoverflow.com/questions/18024298/regular-expression-starting-and-ending-with-a-character-string) – nvidot Nov 19 '20 at 13:41

1 Answers1

0

Tested and working :)

Steps :

  • Remove all spaces.
  • Split when there is a ",".
  • Create an array and fill it only with url finishing with .m3u8.
  • You get your ".m3u8" urls :).

<?php

$string = "http://exemple.com/file/01.mp4 ,http://exemple.com/file/02.m3u8 , http://exemple.com/file/01.mp3 ,http://exemple.com/file/05.m3u8 ,http://exemple.com/file/02.mp4" ;

$string = str_replace(' ', '', $string);

$urls = explode( ",", $string);

$result = [];
foreach ($urls as $url){
 if( substr($url, -5) == ".m3u8") array_push($result, $url);
}

var_dump($result);
Velome
  • 158
  • 1
  • 1
  • 15
  • Is it possible to modify this code to do the same work? Your style is good, but the text problem may be messy. preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $string, $match); – MR ACHRAF DZ Nov 18 '20 at 20:59