In MySQL, how to extract an URL from a long text string?
This isn't in MySQL, but if I understand your question correctly, you seem like you're talking about doing this within a php file by doing something like the following:
<?php $url = '<input type="hidden" name="prtks" value="http://domainname/folder/filename.mp3"/>'; $parts = explode('"', $url); echo 'URL: '.$parts[5];?>
This uses php explode()
to break the string into an array based on " characters.
You can then just echo the 6th part ($parts[5]
)
Why not just use a simple regex to extract the url ?
$str = '<input type="hidden" name="prtks" value="http://domainname/folder/filename.mp3"/>';preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i', $str, $match, PREG_PATTERN_ORDER);var_dump($match[0][0]);// string 'http://domainname/folder/filename.mp3' (length=37)
<?phpfunction urltxt($string){$regex = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i"; ///(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";preg_match_all($regex, $string, $matches); return $matches[0];}$url = urltxt($sqlresult);echo $url[0];//this way its more easier i guess?>