Get text segment from PHP Variable

I’m working on this social media website and want to put a video in a user’s post if they post a YouTube link. The code is complicated but the youtube player is called [php]include(‘video.php?v=***********’)[/php]. The *'s stand for the video code. For example. video.php?v=3JV74i4yvcA, or video.php?v=Swfu7aHuxkM.

Code for comment generator:
[php]

<?php while($info = mysql_fetch_array( $data )) { echo '
'; echo''; echo"
".$info['UserName'] . "
"; echo "
".$info['Date'] . "
"; echo "
".$info['Message'] . "
"; echo '
'; echo '
'; } ?>

[/php]

This is put through javascript and it outputs to the main page. What I want to happen is if a youtube link exist in the Message/comment it will include the video.php file with the correct video.

You’d probably want to use preg_match, but I don’t know what the pattern would be for it.

Give this a try and see if it does what you need it to.

[php]<?php
$info[‘Message’] = ‘this is a test message with a youtube link like: http://www.youtube.com/watch?v=A4Mtgwfqzy0’;

// The Regular Expression filter out youtube links
$regex="~http(?:s?)://(?:www.)?youtu(?:be.com/watch?v=|.be/)([\w-]+)(&(amp;)?[\w?=‌​]*)?~";

// Check your message, $url will be an array, item [0] will contain the full link, item[1] will contain the code.
if(preg_match($regex, $info[‘Message’], $url)) {
print_r($url);
}
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service