Php Function, to extract link from short text
I am looking fo a function, to extract the mp3 source of this snippet of code:
http://www.xy开发者_JAVA技巧z.com/previews/speed_racer_preview.mp3 1930510 audio/mpeg
i only need that part:
http://www.xyz.com/previews/speed_racer_preview.mp3
This part always has the same structure, only the source changes.
If it's always formatted like that just use explode (it breaks a string into an array on a given character, in this case space):
$arr = explode(' ',$the_data_string);
$URL = $arr[0];
Hope this helps,
Lemiant
PS for more info you can read the manual.
echo substr($str, 0, strpos($str, ' '));
$arr = explode(" ",$url);
the explode method return an array so:
$finalurl = $arr[0]
A regex you could use with one of PHP's regex functions (you may need to fiddle with it a bit, I just made this in BareGrep, have not written a full PHP function for you, but this should get you started, the quotes are not part of the regex of course):
" ([A-Za-z0-9_//:.]+[.]mp3) "
Using a regular expression. All matches are saved into $result array.
$res = preg_match("/http:\/\/[^\s]+\.mp3/", $source, $result);
print_r($result); // Each index will be a different matched mp3, if there is more than one
The return value is a bool indicating if the match was positive.
精彩评论