如何通过表单提交到数据库将youtube短网址更改为可嵌入的值?

如何通过表单提交到数据库将youtube短网址更改为可嵌入的值?

问题描述:

i'd like to be able to enter a youtube video's short url into a form input eg;

'http://youtu.be/rAndoMText'

and upon 'submit' perform three functions in the php file that is processing the form:

replace:

'http://youtu.be/'

with:

'<a class='youtube' href="http://www.youtube.com/embed/'

and add:

'?rel=0&amp;wmode=transparent">link</a>'

to the end of the new value

and then submit the final value to the database.

my current php file is below and it's 'logic' is based on my basic knowledge of php and searching for solutions on the internet and i don't expect it to be correct.

it does check out in online syntax checkers though and is sending the other form inputs to the database, just not the one i am trying to modify.

<?php

$original_link = $_POST["link"];
$prepender = "<a class='youtube' href=\"http://www.youtube.com/embed/\"";
$appender = "?rel=0&amp;wmode=transparent\">link</a>";

$modified_link = str_replace ("http://youtu.be/","$prepender","$original_link");

$modified_link . "" . $appender = $final_value;

$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

$sql="INSERT INTO data (fieldone, fieldtwo, fieldthree, fieldfour, link)
VALUES
('$_POST[fieldone]','$_POST[fieldtwo]','$_POST[fieldthree]','$_POST[fieldfour]',
'$final_value')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header ('Location: http://return-to-your-website.com');

mysql_close($con);
?>

i also tried using this in the 'modified_link' area above but it returned the same results:

$modified_link = "";
ob_start (); 
{
echo str_replace ("http://youtu.be/","$prepender","$original_link");
$modified_link = "ob_get_contents ()";
}
ob_end_clean (); 
$modified_link . "" . $appender = $final_value;

thank you.

Here is the code that will give you your desired link:

$link_provided_by_user = 'http://youtu.be/rAndoMText';
$random_text = array_pop(explode('/',$link_provided_by_user)); // get the text after the last slash
$link_you_want = '<a class=\'youtube\' href="http://www.youtube.com/embed/'.$random_text.'?rel=0&amp;wmode=transparent">link</a>';

echo $link_you_want; // ex: <a class='youtube' href="http://www.youtube.com/embed/rAndoMText?rel=0&amp;wmode=transparent">link</a>

Please read about SQL Injections.