Jump to content

Converting youtube url's to embeded video problems


jkewlo

Recommended Posts

Hello everyone

 

    I am working on a blog system. and I am having problems with the youtube video embed I have found some script online that kinda works

 

If I post more then one youtube link the player shows up.. but it says movie not loaded. if I post just one video it just shows the url to the video on youtube.

 

here is my youtube_function.php

 

<?PHP
//convert youtube links into embede

function show_youtube($text)
{
$VID_WID = 320;
$VID_HEI = 240;

for ($k=0; $k<9; $k++)
{
	$text .= "";
	$find = 'youtube.com/watch?v=';
	$pos = strpos($text, $find);
	if ($pos == false)
	{
		break;
	}
	$len = strlen($text);
	for ($i=$pos; $i>=0; $i--)
	{
		if (substr($text, $i, 6) == 'http://')
		{
			$pos1 = $i;
			break;
		}
	}
	for ($i=$pos; $i<$len; $i++)
	{
		if (in_array($text[$i], array('&', ' ', "\r", "\n", ',', "\t")))
		{
			$pos2 = $i;
			break;
		}
	}
	$link1 = substr($text, $pos1, $pos2 - $pos1);
	$link2 = str_replace('/watch?v=', '/v/', $link1);

	$embed =	'<object width="' . $VID_WID . '" height="' . $VID_HEI . '">'.
				'<param name="movie" value="' . $link2 . '"></param>'.
				'<param name="allowFullScreen" value="true"></param>'.
				'<param name="allowscriptaccess" value="always"></param>'.
				'<embed src="' . $link2 . '" type="application/x-shockwave-flash" '.
				'allowscriptaccess="always" allowfullscreen="true" '.
				'width="' . $VID_WID . '" height="' . $VID_HEI . '"></embed></object>';

	$text = str_replace($link1, $embed, $text);
}
return $text;
}
?>

 

and I have index.php

<?php
session_start();
include ("includes/includes.php");
include ("includes/youtube_function.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link rel="stylesheet" href="includes/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
<title>Simple Blogging System</title>

</head>

<body>
<div id="wrap">
<div id="header"><h1>Simple Blogging System<br /><font size="6">*Keep it simple*</font></h1></div>
<div id="nav">
<center>
Link Link Link
</center>
</div>
<div id="main">
  <div id="blogPosts">
<?php

$blogPosts = GetBlogPosts();

foreach ($blogPosts as $post)
{
	echo "<div class='post'>";
	echo "<h2>" . $post->title . "</h2>";
	echo show_youtube($post->post, $text);
	echo "<span class='footer'>Posted By: " . $post->author . " Posted On: " . $post->datePosted . " Tags: " . $post->tags . "</span>";
	echo "</div>";

?>
    <br />
    <?php
    }
?>
</div>
</div>
<div id="sidebar">
<h2>Archive</h2>

<p> old </p>
<p> old </p>
<p> old </p>
</div>
<div id="footer">
<p>Simple Blogging System</p>
</div>
</div>
</body>

</html>

 

so as you can see I have show_youtube($post->post, $text); I have tried this just out of curiosity to see if I could fix this myself. but It seems like the video does not want to run at all... I am confused with this and some help would be greatly appreciated.

 

Thank you in advance

 

 

Link to comment
Share on other sites

nope a new function would be fine.

 

ill post the rest of my functions as well

 

 

This is

 

includes/includes.php

<?php
include 'blogpost.php';

// Change this info so that it works with your system.
$connection = mysql_connect('localhost', '***', '***') or die ("<p class='error'>Sorry, we were unable to connect to the database server.</p>");
$database = "grind3r_blog";
mysql_select_db($database, $connection) or die ("<p class='error'>Sorry, we were unable to connect to the database.</p>");

function GetBlogPosts($inId=null, $inTagId =null)
{
if (!empty($inId))
{
	$query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC"); 
}
else if (!empty($inTagId))
{
	$query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
}
else
{
	$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");
}

$postArray = array();
while ($row = mysql_fetch_assoc($query))
{
	$myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['postfull'], $row["author_id"], $row['date_posted']);
	array_push($postArray, $myPost);
}
return $postArray;
}
?>

 

and blogpost.php

<?php

class BlogPost
{

public $id;
public $title;
public $post;
public $author;
public $tags;
public $datePosted;
public $archive;



function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null, $inArchive=null)
{
if (!empty($inId))
{
	$this->id = $inId;
}
if (!empty($inTitle))
{
	$this->title = $inTitle;
}
if (!empty($inPost))
{
	$this->post = $inPost;
}

if (!empty($inDatePosted))
{
	$splitDate = explode("-", $inDatePosted);
	$this->datePosted = $splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];
}

if (!empty($inAuthorId))
{
	$query = mysql_query("SELECT first_name, last_name FROM people WHERE id = " . $inAuthorId);
	$row = mysql_fetch_assoc($query);
	$this->author = $row["first_name"] . " " . $row["last_name"];
}

$postTags = "No Tags";
if (!empty($inId))
{
	$query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);
	$tagArray = array();
	$tagIDArray = array();
	while($row = mysql_fetch_assoc($query))
	{
		array_push($tagArray, $row["name"]);
		array_push($tagIDArray, $row["id"]);
	}
	if (sizeof($tagArray) > 0)
	{
		foreach ($tagArray as $tag)
		{
			if ($postTags == "No Tags")
			{
				$postTags = $tag;
			}
			else
			{
				$postTags = $postTags . ", " . $tag;
			}
		}
	}
}
$this->tags = $postTags;
}

}

?>

 

I hope this helps you some.. This is my first time trying to write a blog system.

 

Link to comment
Share on other sites

For this function to work the Youtube URL's should be full URL's, for example http://www.youtube.com/watch?v=m4PQyOOuxBU

 

<?php

  function youtubeVideos($inputString) {

    $videoWidth  = 320;
    $videoHeight = 240;

    $string  = explode('http://www.youtube.com/watch?v=',$inputString);
    $count   = count($string);
    $videoArray = array();
    $finalArray = array();
    $outputString = $string[0];

      for($i=1; $i<$count; $i++) {
        array_push($videoArray, substr($string[$i],0,11));
      }

    $videoCount = count($videoArray); 

      for($i=0; $i<$videoCount; $i++) {
        $embed = '<object width="' . $videoWidth . '" height="' . $videoHeight . '">'.
                 '<param name="movie" value="http://www.youtube.com/v/' . $videoArray[$i] . '"></param>'.
                 '<param name="allowFullScreen" value="true"></param>'.
                 '<param name="allowscriptaccess" value="always"></param>'.
                 '<embed src="http://www.youtube.com/v/' . $videoArray[$i] . '" type="application/x-shockwave-flash" '.
                 'allowscriptaccess="always" allowfullscreen="true" '.
                 'width="' . $videoWidth . '" height="' . $videoHeight . '"></embed></object>';
        array_push($finalArray, $embed);
      }

      for($i=1; $i<$count; $i++) {
       $outputString .= $finalArray[$i-1].substr($string[$i],11);
      }

    if($outputString) {
      return $outputString;
    } else {
      return $inputString;
    }
  }

  $myPost = 'BarnYard, such a funny but compelling movie.
I love one of the main characters Ben, such a role model for young kids.
This movie also has a great cover of the classic Johnny Cash song "I won\'t back down" listen to it below. 
http://www.youtube.com/watch?v=m4PQyOOuxBU
(I love that song to be honest)';

  echo nl2br(youtubeVideos($myPost));

?>

 

Try it out and tell me how it goes bud.

 

Regards, Paul.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.