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="" 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'); ?>


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:

<a class="media" href="">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="<?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);


			return '';


That is exactly what I was saying… good work