Reputation: 414
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;
}
Upvotes: 0
Views: 520
Reputation: 4143
Your pattern doesn't match the entire string, and preg_replace
will only replace what your pattern matches. You can use this pattern instead:
'#^http://(www\.)?vimeo\.com/(\w+)(&.*)?$#i'
Upvotes: 0
Reputation: 1009
edit: typo + backslash
You can use backreference for your (.*) values in your case
#http://(www\.)?vimeo\.com/(\w+)?#i
you can use \\1 for getting www. and \\2 for getting the values which you catch by (\w+)
or was this not what you want?
Upvotes: 0
Reputation: 4373
&
isn't part of \w
that's it. I'd go with \S
(not a space) instead.
#http://(www\.)?vimeo\.com/(\S+)?#i
Upvotes: 1