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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?