0

Hi I have a code which can show youtube videos if there are any links to youtube in the text

like for example the text which has multiple links

http://www.youtube.com/watch?v=-LiPMxFBLZY testing http://www.youtube.com/watch?v=Q3-l22b_Qg8&feature=related

example at:- pradyut.dyndns.org

the question has three parts which can be done using regex..

1 > getting the urls one by one for checking

2>checking if they youtube urls

3>converting the urls to youtube objects or iframes...

the second and the third are almost done with regex .. any improvements are welcome...

the most cumbersome is part one of the code which is to be done using regex...

this text i m forwarding to the function...

   function to_youtubelink(text)
            {
                if ( text.indexOf ('<') > 0 || text.indexOf ('"') > 0 || text.indexOf ('>') > 0 )
                    return text;
                else
                {
                    var obj_text = new Array();
                    var oi = 0;
                    while(text.indexOf('http://') >=0)
                    { //getting the paths
                        var si = text.indexOf('http://');
                        var gr = text.indexOf('\n', si);
                        var sp = text.indexOf(' ', si);
                        var ei;
                        if ( gr > 0 || sp > 0  )
                        {
                            if ( gr >0 && sp > 0 )
                            {
                                if ( gr < sp )
                                {
                                    ei = gr ;
                                }
                                else
                                {
                                    ei = sp ;
                                }
                            }
                            else if ( gr > 0)
                            {
                                ei = gr;
                            }
                            else
                            {
                                ei = sp;
                            }
                        }
                        else
                        {
                            ei = text.length;
                        }
                        var it = text.substring(si,ei);

                        if ( it.indexOf('"') > 0)
                        {
                            it.substring(0, it.indexOf('"') );
                        }

                        if(ei < 0)
                            ei = text.length;
                        else
                            ei = text.indexOf(' ', si) ;

                        obj_text[oi] = it;

                        text = text.replace( it, '[link_service]');
                        oi++;

                    }

                    var ob_text = new Array();
                    var ob =0;
                    for (oi=0; oi<obj_text.length; oi++)
                    {
                        if ( is_youtubelink( obj_text[oi] ) )
                        {
                            ob_text[ob] = to_utubelink(obj_text[oi]);

                            ob++;
                        }
                    }
                    oi = 0;
                    while ( text.indexOf('[link_service]') >=0  )
                    {

                        text = text.replace( '[link_service]', obj_text[oi]);

                        oi ++;
                    }

                    for (ob=0; ob<ob_text.length; ob++)
                    {
                        text = text +"\n\n" + ob_text[ob];
                    }

                    return text;
                }
            }

       function is_youtubelink(text)
            {
                var matches = text.match(/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/);
                if (matches) {
                    return true;
                } else {
                    return false;
                }

            }

            function to_utubelink(text)
            {
                var video_id = text.split('v=')[1];
                    var ampersandPosition = video_id.indexOf('&');
                    if(ampersandPosition != -1)
                    {
                        video_id = video_id.substring(0, ampersandPosition);
                    }

                    text = "<iframe title=\"YouTube video player\" class=\"youtube-player\" type=\"text/html\" width=\"425\" height=\"350\" src=\"http://www.youtube.com/embed/" + video_id + "\" frameborder=\"0\"></iframe>"
                    return text;
            }

now i m getting the output properly...

but i was thinking if the code could be done better and simplified using regex

...especially getting the urls part...

thanks

2 Answers 2

1

Try something like this:

/**
 * Extracts a youtube id from a string
 * 
 * @param string
 *
 * @return mixed
 */

function youtubeIDextract(text) { 
    var replace = "$1";

    if (!text.match(/http:\/\/www\.youtube[^"]+/))
        return false;

    if (text.match(/^[^v]+v.([^&^=^\/]{11}).*/))
        return text.replace(/^[^v]+v.([^&^=^\/]{11}).*/,replace); 
    else if (text.match(/^[^v]+\?v=([^&^=^\/]{11}).*/))
        return text.replace(/^[^v]+\?v=([^&^=^\/]{11}).*/,replace); 
    else
        return false;
}

A working example can be seen here: http://jsfiddle.net/6PRVp/2/

If you want to simply check if there is a youtube link in there somewhere, do this:

if (youtubeIDextract(text)) {...}

Notice that this also checks if there are 11 characters in the video id (without that, it would be a malformed youtube link).

Sign up to request clarification or add additional context in comments.

12 Comments

i m already doing this in the function "to_utubelink"(the last function od my code).. may this is a better implmentation...
@Pradyut: hold on, tweaking it now.
i dont know much about regex although but take a look for matching if the url is from youtube with a wider regex in my function "is_youtubelink()"... thanks...
in the link there u r having only one youtube link... put a run with my text with multiple youtube links...no hurry btw:D
@Pradyut: hm yes, it appears to not match all cases. I'm trying to figure this out at the moment.
|
0

Try this one:

function to_youtubelink(a) {
if (a.indexOf("<") > 0 || a.indexOf('"') > 0 || a.indexOf(">") > 0) return a;
var b = Array(),
    c = 0;
while (a.indexOf("http://") >= 0) {
    var g, d = a.indexOf("http://"),
        e = a.indexOf("\n", d),
        f = a.indexOf(" ", d);
    g = e > 0 || f > 0 ? e > 0 && f > 0 ? f > e ? e : f : e > 0 ? e : f : a.length;
    var h = a.substring(d, g);
    h.indexOf('"') > 0 && h.substring(0, h.indexOf('"')), g = 0 > g ? a.length : a.indexOf(" ", d), b[c] = h, a = a.replace(h, "[link_service]"), c++
}
var i = Array(),
    j = 0;
for (c = 0; b.length > c; c++) is_youtubelink(b[c]) && (i[j] = to_utubelink(b[c]), j++);
c = 0;
while (a.indexOf("[link_service]") >= 0) a = a.replace("[link_service]", b[c]), c++;
for (j = 0; i.length > j; j++) a = a + "\n\n" + i[j];
return a
}
function is_youtubelink(a) {
var b = a.match(/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/);
return b ? !0 : !1
}
function to_utubelink(a) {
var b = a.split("v=")[1],
    c = b.indexOf("&");
return c != -1 && (b = b.substring(0, c)), a = '<iframe title="YouTube video player" class="youtube-player" type="text/html" width="425" height="350" src="http://www.youtube.com/embed/' + b + '" frameborder="0"></iframe>'
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.