douan0729 2012-09-23 04:17
浏览 12
已采纳

如何通过表单提交到数据库将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.

展开全部

  • 写回答

1条回答 默认 最新

  • donglu5612 2012-09-23 05:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部