douan0729 2012-09-23 12: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 13: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.

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

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号