YouTube Embed Code

I have a database text field that I use to store YouTube embed code. However, when I try to update the record using a CActiveForm textArea the HTML embed code is lost. For example, if I enter the following only the ‘then some ordinary text’ is saved’:

<iframe width="420" height="315" src="http://www.youtube.com/embed/uAY_Ow22D_A" frameborder="0" allowfullscreen></iframe>

then some ordinary text

I assume that this is because the input is being purified, but I cannot find out how to switch this off. Any help would be appreciated. For you info the input field is as follows:




	<div class="row">

	                <?php echo $form->labelEx($model,'video_one_code'); ?>

			<?php echo $form->textArea($model,'video_one_code',array('rows'=>6, 'cols'=>50)); ?>

			<?php echo $form->error($model,'video_one_code'); ?>

	</div>



Another solution would be that instead of inserting the whole code of the youtube video, you just insert the link which could be easily validated. On display, you include the link the appropriate HTML code. This way you don’t have to worry for future malicious code if you are not the one using the CMS… Just an idea

Check out jQuery Media:

http://jquery.malsup.com/media/


<a class="media" href="sample.mov">My Quicktime Movie</a>

<a class="media" href="sample.swf">My Flash Movie</a> 

<a class="media" href="sample.wma">My Audio File</a> 

I am not saying it would work in your situation, but it looks a lot less complicated to me. ;)

The benefits is that you don’t have to store the embeding markup code in your db.

The above comments gave me the idea for my solution:

  1. Store the YouTube link.

  2. In my view put the following code:




<iframe width="420" height="315" src="http://www.youtube.com/embed/<?php echo $model->getYouTubeCode($model->youTubeLink)?>" frameborder="0" allowfullscreen></iframe>



  1. In my model have a function:



	

	/**

	 * Get YouTube code from YouTube link

	 * @param string link

	 * @return string YouTube code

	 */

	public function getYouTubeCode($link)

	{

		$pos = strrpos ($link, '/');

		if($pos !== false) 

			return substr($link, $pos);

		else

			return '';

	}



That is exactly what I was saying… good work