php - How strip a section of a url away to get the remaining unique ID -
hi have variable youtube url user-generated. there lot of threads on here regex , not, being executed in js. want run server-side in php, here simple function:
function youtubeid($url){ $url = preg_replace('http://youtu.be/ regex goes here', '', $url); return $url; }
markup:
<a href="http://youtu.be/yhxhvmkxnky"><img src="http://img.youtube.com/vi/'.youtubeid($variable).'/0.jpg" /><img src="play.png" alt="watch video" title="watch video"/></a>
question: know proper regex http://youtu.be/ ?
i'm sure quick answer, i'm not savvy regex. on side note,does know of resources can teach myself regex don't have ask silly syntax questions this? cheers.
why use (relatively slow) regex @ all?
$url = substr($url, strlen('http://youtu.be/'));
obviously have set start point no need calculate each time call function, doing here demonstrate idea.
edit:
in case there's reason required use regex:
$url = preg_replace('#http\://youtu\.be/#', '', $url);
cheers
Comments
Post a Comment