开发者

Replace Youtube links in text with a specific embed code

Here's my problem. I have a textarea in my form, where users can submit youtube video link开发者_运维技巧s . I need to parse the text and replace the youtube link with their embed equivalents. an example:

Initial URL in text: "blah blah http://www.youtube.com/watch?v=PLs5HN7FS0w blah blah" Final text before storing in mysql db: "blah blah [youtube=http://www.youtube.com/watch?v=PLs5HN7FS0w] blah blah"

Any idea how to do this for every link in the textbox with php?


Use regular expressions:

$target = 'blah blah http://www.youtube.com/watch?v=PLs5HN7FS0w blah blah';
$target = preg_replace('/(http:\/\/www\.youtube[^ ]*)/', '[youtube=$1]', $target);

This matches everything that starts with http://www.youtube and does not contain spaces, and captures it into $1. Then it replaces the matched part with [youtube=$1] where $1 is the URL


I'd go for a slightly more complex regExp, to be a bit safer and to drop all extra YT params that you don't really need (and users will paste them, you can bet on that):

$url = 'blabla http://www.youtube.com/watch?v=I6CyoYSlL-M&feature=rec-LGOUT-exp_stronger_r2-2r-26-HM aaaa bbb ccc';
echo preg_replace('|http://www\.youtube\.com/watch\?.*\bv=([^&]+)[^\s]*|', '[youtube=http://www.youtube.com/watch?v=$1]', $url);

Also this BBcode shouldn't really require this "http://www.youtube..." part of the url, it's always the same ?!


I got slightly bored, and came up with a ridiculously clunky, though relatively simple and functional, jQuery means of doing this:

$('form').submit(

function() {
    var text = $('#text').val().split(' ');
    for (i = 0; i < text.length; i++) {
        var test = text[i].indexOf('http://www.youtube.com/watch');
        if (test != -1) {
            text[i] = '[youtube=' + text[i] + ']';
        }
    }
    text = text.join(' ').trim();
    $('#text').val(text);
    return false;
});

JS Fiddle demo.


Edited to add a necessary sanity check to the function:

$('form').submit(

function() {
    var text = $('#text').val().split(' ');
    for (i = 0; i < text.length; i++) {
        var test = text[i].indexOf('http://www.youtube.com/watch');
        var check = text[i].indexOf('[youtube=');
        if (test != -1 && check == -1) {
            text[i] = '[youtube=' + text[i] + ']';
        }
    }
    text = text.join(' ').trim();
    $('#text').val(text);
    return false;
});

JS Fiddle of sanity-checking demo.

Basically, in the first function it was possible to simply keep re-submitting the form and prepending '[youtube=' and appending a closing ']'. This latter approach absolves me of some of the stupidity. OF course I could have just checked the the indexOf() value of 'test' is equal to 0, so it was at the beginning of the string. Which I might do, if I revisit this.


Edited because, sometimes, I just can't leave things be...

$('form').submit(

function() {
    var text = $('#text').val().split(' ');
    for (i = 0; i < text.length; i++) {
        var test = text[i].indexOf('http://www.youtube.com/watch');
        if (test === 0) {
            text[i] = '[youtube=' + text[i] + ']';
        }
    }
    text = text.join(' ').trim();
    $('#text').val(text);
    return false;
});

Updated JS Fiddle demo.


Edited (again) because I can't leave things be, and because I figured (in the comments) that the jQuery example should be doable in php (more or less directly). And so here it is:

$text = $_POST['text']; // from the textarea of name="text"

$text = explode(" ",$text);
for ($i=0; $i < count($text); $i++) {
    if (stripos($text[$i], 'http://www.youtube.com/watch') === 0) {
        $text[$i] = '[youtube=' . $text[$i] . ']';
    }
}
$text = implode(" ",$text);

I don't know of anywhere that hosts php demos publically, and safely, so I can't demo this, but I think it should work. Although using a foreach() might well have been easier than a for() loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜