Remove part of data entered by the user

Asked

Viewed 37 times

0

I am developing a website for sending videos to teachers, for this, I made a form with the following fields:

inserir a descrição da imagem aqui

Considering I’m still learning PHP. I would like to know if it is possible to have the youtube link inserted by the user filled in the following way in mysql:

Video link inserted by the user: https://www.youtube.com/watch?v=KBk-ayp6yyU

Apart from the link, I would like only mysql to be the video code, as an example:

Kbk-ayp6yyU

Another example:

Link: https://www.youtube.com/watch?v=Nzta28cfu3w

In mysql: Nzta28cfu3w

How can I do this? Below is the registration:

<?php
include("conexao.php");

$nome_video = $_POST['nome_video'];
$disciplina  = $_POST['disciplina'];
$link_video  = $_POST['link_video'];
$coment_video  = $_POST['coment_video'];
$arquivo = $_FILES["arquivo"];

if (is_uploaded_file($_FILES['arquivo']['tmp_name'])){

    $extensao = strtolower(substr($_FILES['arquivo']['name'], -4)); //pega a extensao do arquivo
    $novo_nome = md5(time()) . $extensao; //define o nome do arquivo
    $diretorio = "upload/"; //define o diretorio para onde enviaremos o arquivo
    move_uploaded_file($_FILES['arquivo']['tmp_name'], $diretorio.$novo_nome); //efetua o upload
      $sql_logar = "INSERT INTO video_monitor (titulo_video, disciplina, link_video, coment_video, arquivo, data) 
      VALUES('$nome_video', '$disciplina', '$link_video', '$coment_video', '$novo_nome', NOW())";
      $exe_logar = mysqli_query($conection, $sql_logar) or die (mysqli_error($conection));
  } else {
      $sql_logar = "INSERT INTO video_monitor (titulo_video, disciplina, link_video, coment_video, arquivo, data)
      VALUES ('$nome_video', '$disciplina', '$link_video', '$coment_video', 'Nenhum arquivo', NOW())";
      $exe_logar = mysqli_query($conection, $sql_logar) or die (mysqli_error($conection));
  }

?>
  • https://stackoverflow.com/questions/412467/how-to-embed-youtube-videos-in-php

1 answer

2


You can extract the ID from Youtube links using PHP’s URL manipulation tools.

$url = $_POST['link_video'];
parse_str( parse_url( $url, PHP_URL_QUERY ), $saida_de_variaveis);
echo $saida_de_variaveis['v'];    // obtém o parâmetro 'v' da url 
  // Saída: NZta28cFU3w

See working on Ideone.

So when you want this ID, you can associate it to a variable with:

$id_do_video = $saida_de_variaveis['v'];

This is because the parse_url parses the input URL and returns the parameters that are inserted into it. The parse_string gets the results for a array called $saida_de_variaveis.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.