I would like to use custom filter to recognise a url from various media sites such as youtube.com and automatically embed the video into the page. I have a similar solution working in vbulletin (AME - Auto Media Embedding) this system uses a regex to catch the youtube url and a regex back reference to capture the relevant part of the url for use in the html to embed the video.
eg the youtube regex is as follows
[http://]*[a-z]*?[\.]?youtube\.[a-z]*?/watch\?v=([A-Z0-9._%-]*)[&\w;=\+_\-]*
this is then turned into the following html embed
<object width="425" height="350">
<param name="movie" value="http://www.youtube.com/v/$p1"></param>
<embed src="http://www.youtube.com/v/$p1" type="application/x-shockwave-flash" width="425" height="350" wmode="transparent"></embed>
</object>
As i'm no expert in php / regex i've been unable to get this system to work with the custom filter - it seems to have trouble with the back reference? I would be really grateful if anyone can help me to get this working as it offers a very simple way of allowing users to insert embeded video into their posts simply by pasting the url of the video from youtube.
thanks
tom
Comments
Comment #1
DementedManiac CreditAttribution: DementedManiac commentedHi Tom,
When building up complex regex's I find it best to start your testing with a simple regex, and get it working, then make small one-step-at-a-time mods until it does everything you want it to do.
Using this aproach, I took your example above, simplified it to
Pattern:
/http:\/\/www\.youtube\.com\/watch\?v=([A-Z0-9]*)/i
Replacement text:
I'll work on it some more when I get a chance, but the regex for the value of v may not always work, I've only tested it so far on one video. Also, this regex doesn't acount for youtube domains other than exactly "www.youtube.com" "au.youtube.com" for example won't be picked up by this.
btw, you need to use $1 not $p1
Hope this helps.
Dawson
Comment #2
apadernoDementedManiac is right; the replacement text uses $p1, not $1 as it should.