I’m having trouble with preg_replace and I’m not sure that I use correct function.
I’m usign function below in order to change youtube links into youtube embed videos. But I couldn’t findout how I only get matched part and remove the rest?
I mean for instance :
http://www.vimeo.com/3124234&feature=1&v=1
when I use this function it’s change to matched part an embed code. But I couldnt achieve to remove “&feature=1” part.
Should I use preg_replace for that or any other function can do what I’m trying?
Cheers
function convert_videos($string) {
$rules = array(
'#http://(www\.)?vimeo\.com/(\w+)?#i' => '<object width="450" height="320" data="http://vimeo.com/moogaloop.swf?clip_id=$2&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1"></object>'
);
foreach ($rules as $link => $player)
$string = preg_replace($link, $player, $string);
return $string;
}
Your pattern doesn’t match the entire string, and
preg_replacewill only replace what your pattern matches. You can use this pattern instead: