Help with regex and preg_replace

Discussion in 'PHP' started by x0x, Oct 8, 2011.

  1. #1
    function youtube($string)
    {
      return preg_replace(
        '#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&?.*?#i',
        '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4" frameborder="0" allowfullscreen></iframe>',
        $string
      );
    }
    PHP:
    This is supposed to take the youtube URL and auto-embed it. The URL should be replaced the the embed code and the rest of the text should remain untouched. Embedding works like a charm, I just need to get rid of the whole URL.

    There seem to be two bugs:

    1.

    youtube('http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded');
    
    // *embedded video*&feature=player_embedded (or amp;feature=player_embedded)
    PHP:
    That part of the URL should also be gone...

    2.

    echo youtube('http://youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded');
    
    // www.youtube.com *embedded video* amp;feature=player_embedded
    PHP:
    Any help would be appreciated!
     
    x0x, Oct 8, 2011 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    This works fine for me in a test.php file removing &feature=player_embedded

    
    <?php
    
    
    function youtube($string)
    {
      return preg_replace(
        '#(http://(www.)?youtube.com)?/(v/|watch\?v\=)([-|~_0-9A-Za-z]+)&feature=player_embedded#i',
        '<iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/$4" frameborder="0" allowfullscreen></iframe>',
        $string
      );
    }
    
    echo youtube('http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded');
    
    ?>
    
    PHP:
     
    MyVodaFone, Oct 9, 2011 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks, but I see you added the variable in the regex. That was a mere example - the variables can have many other names and there can be more than one.
     
    x0x, Oct 9, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Ok so how about this way, getting the id first then position it into the iframe url
    
    <?php
    
    function youtube($string)
    {
    
    preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);
    
    echo "<iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe>";
    
    } 
    
    youtube('http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded');
    
    ?>
    
    PHP:
     
    MyVodaFone, Oct 9, 2011 IP
  5. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #5
    x0x, Oct 9, 2011 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    MyVodaFone, Oct 9, 2011 IP
  7. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Actually that was my code. It didn't display the function name you tube for some reason, so lets call it yout

    yout ('check out this video http://www.youtube.com/watch?v=8uTTh...layer_embedded please');
    
    // returns only the embedded video
    // should return check out this video *embedded video* please
    PHP:
     
    x0x, Oct 9, 2011 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    Maybe like this ?
    
    <?php
    
    function youtube($string)
    {
    
    preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);
    
    $title = "Check out this video";
    $comment = "Please";
    
    echo "$title<br /> <iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe><br />$comment";
    
    } 
    
    youtube('http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded');
    
    ?>
    
    PHP:
     
    MyVodaFone, Oct 9, 2011 IP
  9. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Hehe.. Man. I love that you are trying help, but the goal is to automatically embed youtube videos from users input. "Check out this video" was just an example what an user might say. It could be anything. The point being that only the URL should be replaced with the embedded video (and whatever variables are at the end of it), and the rest of the text should not be touched.
     
    x0x, Oct 9, 2011 IP
  10. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #10
    I understand now... but your not telling us how they input stuff, are you using $_POST[title] $_POST etc.. [php] $title = $_POST['tit...If I'm still way off the mark, I'll give up:)
     
    MyVodaFone, Oct 9, 2011 IP
  11. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #11
    There is only one variable and that is the $string. It's all crammed in there. Just like a forum post with an URL to a youtube video in it. My goal is to auto-embed that URL without touching the rest of the text.

    Thanks for trying to help, I appreciate it.
     
    x0x, Oct 9, 2011 IP
  12. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #12
    This seems to work, first we get the id, then we remove the youtube url altogether.

    You can add more symbols in here is required [=a-z0-9&_]
    
    <?php
    
    function youtube($string)
    {
    preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);
      return preg_replace(
        '#(http://www\.youtube\.com/watch\?[=a-z0-9&_]+)#i',
        "<iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe>",
        $string);
    }
    
    
    echo youtube('check out this video http://www.youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded please');
    
    ?>
    
    PHP:
     
    MyVodaFone, Oct 10, 2011 IP
  13. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #13
    That works like a charm. Except there is one small bug left in there. It doesn't work if the url only has http:// or www. It only seems to work with http://www.

    This one will probably take me several hours to figure out as I'm a total regex noob, but feel free to help me out. :)
     
    x0x, Oct 10, 2011 IP
  14. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #14
    This should solve that, its just like your first code
    
    <?php
    
    function youtube($string)
    {
    preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);
      return preg_replace(
        '#(http://(www.)?youtube\.com/watch\?[=a-z0-9&_]+)#i',
        "<iframe title=\"YouTube video player\" width=\"480\" height=\"390\" src=\"http://www.youtube.com/embed/$match[2]\" frameborder=\"0\" allowfullscreen></iframe>",
        $string);
    }
    
    
    echo youtube('check out this video http://youtube.com/watch?v=8uTThzrBYwg&feature=player_embedded please');
    
    ?>
    
    PHP:
     
    MyVodaFone, Oct 10, 2011 IP
  15. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #15
    Thanks a lot!
     
    x0x, Oct 10, 2011 IP